String内置

 1 # String的内置方法
 2 
 3 st='hello kitty {name} is {age}'
 4 
 5 print(st.count('l'))       #  统计元素个数                #2
 6 print(st.capitalize())     #  首字母大写                 #Hello kitty {name} is {age}
 7 print(st.center(50,'#'))   #  居中                         ############hello kitty {name} is {age}############
 8 print(st.endswith('tty3')) #  判断是否以某个内容结尾           False
 9 print(st.startswith('he')) #  判断是否以某个内容开头               True
10 print(st.expandtabs(tabsize=20))                    #hello kitty {name} is {age}
11 print(st.find('t'))        #  查找到第一个元素,并将索引值返回          8
12 print(st.format(name='alex',age=37))  # 格式化输出的另一种方式   待定:?:{}  hello kitty alex is 37
13 print(st.format_map({'name':'alex','age':22}))      #hello kitty alex is 22
14 print(st.index('t'))        #8   查找到第一个元素,并将索引值返回 
15 print('asd'.isalnum())      #检查判断字符串是否包含字母数字字符。 True
16 print('12632178'.isdecimal())# 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象  True
17 print('1269999.uuuu'.isnumeric())  #Unicode数字,全角数字(双字节),罗马数字,汉字数字
18 print('abc'.isidentifier())#False
19 print('Abc'.islower())#False  全是大写
20 print('ABC'.isupper())#true  全是小写
21 print('  e'.isspace())#False    方法检测字符串是否只由空格组成
22 print('My title'.istitle())#False 单词的第一个字母是否大写
23 print('My tLtle'.lower())#my tltle
24 print('My tLtle'.upper())#MY TLTLE
25 print('My tLtle'.swapcase())#用于对字符串的大小写字母进行转换 mY TlTLE
26 print('My tLtle'.ljust(50,'*'))#My tLtle******************************************
27 print('My tLtle'.rjust(50,'*'))#******************************************My tLtle
28 print('	My tLtle
'.strip())#My tLtle
29 print('	My tLtle
'.lstrip())#My tLtle
30 print('	My tLtle
'.rstrip())#        My tLtle
31 print('ok')  #ok
32 print('My title title'.replace('itle','lesson',1)) #My tlesson title
33 print('My title title'.rfind('t'))#11
34 print('My title title'.split('i',1))#['My t', 'tle title']
35 print('My title title'.title())#My Title Title
36 
37 
38 #摘一些重要的字符串方法
39 #1 print(st.count('l'))
40 # print(st.center(50,'#'))   #  居中
41 # print(st.startswith('he')) #  判断是否以某个内容开头
42 # print(st.find('t'))
43 # print(st.format(name='alex',age=37))  # 格式化输出的另一种方式   待定:?:{}
44 # print('My tLtle'.lower())
45 # print('My tLtle'.upper())
46 # print('	My tLtle
'.strip())
47 # print('My title title'.replace('itle','lesson',1))
48 # print('My title title'.split('i',1))
原文地址:https://www.cnblogs.com/zerozs/p/8168672.html