#字符串test = 'alex'#capitalize() ---首字母大写print(test.capitalize())#lower() ---字母转换为小写test1 = 'Alex'print(test1.lower())#center() ---返回一个长度为width,两边用fillchar(单字符)填充# 的字符串,即字符串str居中,两边用fillchar填充。若字符串的长# 度大于width,则直接返回字符串strprint(test.center(10))print(test.center(10,'#'))#count() ---计算字符在字符串中出行的次数,区分大小写l = 'eileen'l1 = 'eileeneileeneileen'print(l.count('e'))print(l1.count('eileen'))print(l1.count('eileen',5))#从第5个位置开始#endwith() ---以什么结尾print(test.endswith('ex'))#startwith() ---以什么开始print(test.startswith('ex'))#find() ---Python find() 方法检测字符串中是否包含子字符串# str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是# 否包含在指定范围内,如果包含子字符串返回开始的索引值,否则# 返回-1l2 = 'today is sunday'print(l2.find('sunday'))#format() ---将一个字符串中的占位符{},替换为指定的值test2 = 'i am {x},today is {y}'v = test2.format(x ='alex',y = 'sunday')print(v)#若占位符为数字,则按照顺序进行替换test3 = 'i am {0},today is {1}'v1 = test3.format('alex','sunday')print(v1)#isalnum() 方法检测字符串是否由字母和数字组成print(test.isalnum())print(test3.isalnum())
来源:https://www.cnblogs.com/lqcjlu/p/12243829.html