zoukankan      html  css  js  c++  java
  • Python字符串全解

    1.字符串大小写转换

    1 def strChange():
    2     str = "niuXinLong@163.com"
    3     print("原字符串:" + str)
    4     print("字母写转换成小写:" + str.lower())
    5     print("字母写转换成大写:" + str.upper())
    6     print("大写转换成小写,小写转换成大写:" + str.swapcase())
    7     print("首字母大写:" + str.title())

    2.字符串测试

     1 def strTest():
     2     str = "Niuxinlong@163.com"
     3     print("原始字符串:" + str)
     4     print("是否全是字母:s.isalpha=%s" % str.isalpha())
     5     print("是否全是数字:s.isdigit=%s" % str.isdigit())
     6     print("是否全是空白字符:s.isdigit=%s" % str.isspace())
     7     print("字母中是否全是小写:s.islower=%s" % str.islower())
     8     print("字母中是否全是大写:s.isupper=%s" % str.isupper())
     9     str = "Niuxinlong"  # 注意此处“Niuxinlong@163.com”是检测不出来的,字符串中字母之间不能包含其他字符(非字母)
    10     print("是否是首字母大写:s.istitle=%s" % str.istitle())

    3.字符串分割与组合

     1 def strSplit():
     2     str = "I love JuJingyi!"
     3     print(str.split())  # 默认分隔符为空白字符,分割为列表形式
     4     print(str.split("i", 2))  # 以字符"i"为分割符,分割2次。如果分割次数超过字符串最多分割的次数则分割最多的次数
     5     strList = ["hello", "Word", "!"]
     6     print("".join(str))  # 分割符.join()实现字符串的组合
     7     print(" ".join(strList))
     8     print("#".join(strList))
     9     str = "Hello!"
    10     print(" ".join(str))  # 如果join()方法的参数是字符串,默认分割符连接每个字符

    4.字符串搜索和替换

    1 def strFind():
    2     str = " I love ju Jingyi! "
    3     print(str.find("i"))  # 打印返回的第一"i"的标号,无则返回-1
    4     print(str.count("i"))  # 计算字符"i"在字符串中出现的次数
    5     print(str.replace("j", "J", 1))  # 将字符串中的小写的"j"替换为大写"J"
    6     print(str.lstrip())  # 去掉字符串左边的空格
    7     print(str.rstrip())  # 去掉字符串右边的空格
    8     print(str.strip())  # 去掉字符串左右的空格

    
    

    if __name__ == "__main__":
      print("---------字符串大小写转换---------")
      strChange()
      print(" ---------字符串测试---------")
      strTest()
      print(" ---------字符串分割与组合---------")
      strSplit()
      print(" ---------字符串搜索和替换---------")
      strFind()

    关联博客(CSDN):https://blog.csdn.net/m0_38022608/article/details/80207546

     
     
  • 相关阅读:
    P1135 奇怪的电梯题解
    P1443 马的遍历题解
    P2392 kkksc03考前临时抱佛脚题解
    P1219 八皇后问题题解
    IDEA导入/导出live templates或者其他设置
    P3743 kotori的设备题解
    带你看论文丨全局信息对于图网络文档解析的影响
    【“互联网+”大赛华为云赛道】EI命题攻略:华为云EI的能力超丰富,助你实现AI梦想
    【“互联网+”大赛华为云赛道】IoT命题攻略:仅需四步,轻松实现场景智能化设计
    只需6步,教你从零开发一个签到小程序
  • 原文地址:https://www.cnblogs.com/qikeyishu/p/8995315.html
Copyright © 2011-2022 走看看