python数据类型之字符串(一)
字符串常用功能
1.capitalize:首字母大写
1 test = "feng" 2 首字母大写 3 v = test.capitalize() 4 print(v)
2.casefold,lower:所有变小写,casefold更牛逼,很多未知的相应变小写
1 v1 = test.casefold() 2 print(v1) 3 v2 = test.lower() 4 print(v2)
3.center:设置宽度,并居中
1 # 20 代指总长度 2 # * 空白未知填充,一个字符,可有可无 3 v = test.center(20,"中") 4 print(v)
>>>中中中中中中中中feng中中中中中中中中
4.count:去字符串中寻找,寻找子序列的出现次数
1 test = "fengfeng" 2 v = test.count('en') 3 print(v) 4 5 test = "fengfeng" 6 v = test.count('en',5,7) 7 print(v)
5.startswith,endswith:以什么什么结尾,以什么什么开始
1 test = "feng" 2 v = test.endswith('en') 3 v = test.startswith('en') 4 print(v)
6.find,index:find从开始往后找,找到第一个后,获取其位置,index找不到报错
1 test = "feng" 2 #未找到 -1 3 v = test.find('en') 4 print(v) 5 6 test = "feng" 7 v = test.index('feng') 8 print(v)
7.format,format_map:格式化,将一个字符串中的占位符替换为指定的值
1 test = 'i am {name}, age {a}' 2 print(test) 3 v = test.format(name='feng',a=19) 4 print(v) 5 6 test = 'i am {0}, age {1}' 7 print(test) 8 v = test.format('feng',19) 9 print(v) 10 11 #格式化,传入的值 {"name": 'feng', "a": 19} 12 test = 'i am {name}, age {a}' 13 v1 = test.format(name='df',a=10) 14 v2 = test.format_map({"name": 'feng', "a": 19})
8.isalnum字符串中是否只包含 字母和数字
1 test = "123" 2 v = test.isalnum() 3 print(v)
每天学习新的知识,会让自己更加充实