1 # -----------  首字母大写  ----------
2 test = "alex is a man"
3 v = test.capitalize()
4 print(v):  Alex is a man
1 # -----------  转换全部字符串为小写  ----------
2 test = "aLex is A man"
3 V1 = test.casefold()  #更加强大,可以处理其他语言体系
4 print(V1)  # alex is a man
5 V2 = test.lower()   #只处理英文字符
6 print(V2)  # alex is a man
1 # -----------  设置宽度,并将内容居中  ----------
2 # 20 代表总长度,如果小于字符串本身长度,则忽略
3 # '+' 表示填充的内容,默认为填充空格,只能为单字符(支持中文)
4 test = "aLex is A man"
5 V1 = test.center(20)
6 print('*'+V1+'*')  #  *   aLex is A man    *
7 V2 = test.center(20,'+')
8 print('*'+V2+'*')  #  *+++aLex is A man++++*
1 # -----------  去字符串中寻找子序列出现的次数  ----------
2 # 从第5个位置(包括5)开始往后找,缺省为从0找起
3 # 直到第14个位置(不包括14)结束,缺省找到末尾
4 test = "aLexisAmanAlexALex"
5 V1 = test.count('ex')
6 print(V1)  #  3
7 V2 = test.count('ex',5, 14)
8 print(V2)  #  1
 1 # -----------  判断是否以特定字符串结尾/开始  ----------
 2 test = "aLex is A man"
 3 V1 = test.endswith('a')
 4 print(V1)  # False
 5 V2 = test.endswith('an')
 6 print(V2)  # True
 7 V1 = test.startswith('a')
 8 print(V1)  # True
 9 V2 = test.startswith('an')
10 print(V2)  # False

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
 1 # -----------  从开始往后找,找到第一个后,获取其位置  ----------
 2 # 返回第一个找到的字符串下标,找不到则返回-1
 3 # 对于查找区间满足左闭后开的原则
 4 test = "aLexaLexaLex"
 5 V1 = test.find("ex")
 6 print(V1)  # 2
 7 V2 = test.find("ex",4,7)
 8 print(V2)  # -1
 9 V3 = test.find("ex",4,8)  # 4<= 查找位置<8
10 print(V3)  # 6
 1 # -----------  格式化1,将字符串中的占位符替换为指定的值  ----------
 2 # 按照占位符名称替换
 3 test = "I am {name},age {a}"
 4 print(test)   # I am {name},age {a}
 5 V1= test.format(name='Alex',a=19)
 6 print(V1)    # I am Alex,age 19
 7 
 8 # -----------  格式化2,将字符串中的占位符替换为指定的值  ----------
 9 # 按照数字顺序替换
10 test = "I am {0},age {1}"
11 print(test)   # I am {0},age {1}
12 V1= test.format('Alex',19)
13 print(V1)    # I am Alex,age 19
1 # -----------  格式化3,将字符串中的占位符替换为指定的值  ----------
2 # 按照占位符名称替换, 字典键值对方式传值
3 test = "I am {name},age {a}"
4 print(test)   # I am {name},age {a}
5 V1= test.format_map({"name":"alex","a":19})
6 print(V1)    # I am Alex,age 19

 

1 # -----------  从开始往后找,找到第一个后,获取其位置  ----------
2 # 返回第一个找到的字符串下标,找不到则程序报错,终止运行 (与find函数的区别)
3 # 对于查找区间满足左闭后开的原则
4 test = "aLexaLexaLex"
5 V1 = test.index("ex")
6 print(V1)  # 2
7 V2 = test.index("8")  # 程序报错
8 print(V2)

 

1 # -----------  字符串中是否只包含字母和数字  ----------
2 # 如果只包含字母和数字,则返回True
3 test = "Alex123+124"
4 V1 = test.isalnum()
5 print(V1)   # False
6 test = "Alex123124"
7 V2 = test.isalnum()
8 print(V2)   # True

 

 1 # -----------  分组格式化函数  ----------
 2 # 按照每n个字符分组,碰到\t时使用空格补全n个字符空间
 3 test = "123\t456789\t12345"
 4 test1 = "1234567891234567891234567"
 5 V1 = test.expandtabs(6)
 6 print(V1)       # 123   456789      12345
 7 print(test1)    # 1234567891234567891234567
 8 test = "Username\tEmail\tPassword\nPeter\tPeter@q.com\t1234\nPeter\tPeter@q.com\t1234\nPeter\tPeter@q.com\t1234"
 9 V1 = test.expandtabs(20)
10 print(V1)
11 # Username            Email               Password
12 # Peter               Peter@q.com         1234
13 # Peter               Peter@q.com         1234
14 # Peter               Peter@q.com         1234

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄