name = "My \tname is {name} and my age is {year} old"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | #大写 print (name.capitalize()) # 首字母大写 打印显示 My name is {name} and my age is {year} old #统计 print (name.count( "a" )) # 统计 a 的个数 #打印显示 5 #中间补齐 print (name.center( 50 , "#" )) #打印显示 ###My name is {name} and my age is {year} old### #判断字符串以什么结尾,正确为true ,错误为false print (name.endswith( "ex" )) #打印显示 False #tab 健补全 print (name.expandtabs(tabsize = 10 )) #10 表示10个字符 #查找字符索引 print (name.find( "M" )) #打印显示 0 #format 格式化 print (name. format (name = 'bob' ,year = 33 )) print (name.format_map({ 'name' : 'jerrt' , 'year' : 27 })) #打印显示 My name is bob and my age is 33 old My name is jerrt and my age is 27 old #如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False print ( 'a31' .isalnum()) #打印显示 True #判断是否为纯英文字符 print ( 'abA' .isalpha()) print ( 'abA1' .isalpha()) #打印显示 True False #判断是否为10进制 print ( '1A' .isdecimal()) print ( '113' .isdecimal()) #打印显示 False True #检测字符串是否只由数字组成 print ( '111' .isdigit()) print ( '1AA' .isdigit()) #打印显示 True False #判断是否为合法的标识符 print ( '1A' .isidentifier()) print ( '_1A' .isidentifier()) False True #方法检测字符串是否只由数字组成。这种方法是只针对unicode对象 print ( 'a AA' .isnumeric()) print ( '11' .isnumeric()) #打印显示 False True #检测字符串是否只由空格组成 print ( 'ssA' .isspace()) print ( 'ssA' .isspace()) print ( ' ' .isspace()) #打印显示 False False True #判断字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False. print ( 'My name is ' .istitle()) print ( 'My' .istitle()) #打印显示 False True #检测字符串中所有的字母是否都为大写 print ( 'MY NAME' .isupper()) print ( 'My Name is' .isupper()) #打印显示 True False #join 方法 用于将序列中的元素以指定的字符连接生成一个新的字符串 print ( "+" .join([ 'a1' , 'b2' , 'c3' ])) #打印显示 a1 + b2 + c3 #ljust 返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。 name = name = "My \tname is {name} and my age is {year} old" print (name.ljust( 50 , "*" )) #打印显示 My name is {name} and my age is {year} old * * * * * * #rjust 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。 print (name.rjust( 50 , "*" )) * * * * * * My name is {name} and my age is {year} old #lower 大写变小写 print ( 'BAG' .lower()) #打印显示 bag #upper 小写变成大写 print ( 'bob' .upper()) #打印显示 BOB #用于截掉字符串左边的空格或指定字符 print ( '\nAlex' .lstrip( 'n' )) #从左边去空格 #打印显示 Alex print ( 'Alex\n' .rstrip( '\n' )) #从右边去空格 #打印显示 Alex #strip 用于移除字符串头尾指定的字符(默认为空格) print ( ' Alex\n' .strip()) #去空格 #打印显示 Alex #replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max print ( 'Bobbb' .replace( 'b' , 'B' , 2 )) print ( 'bob' .replace( 'b' , 'B' )) #打印显示 BoBBb BoB #split 通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 print ( 'ljack lex lbob ltim ' .split( 'l' )) print ( '1+2+3+4' .split( '+' )) #按照+ 区分 #打印显示 [' ', ' jack ', ' ex ', ' bob ', ' tim '] [ '1' , '2' , '3' , '4' ] #title 标题 print ( 'hi world' .title()) #打印显示 Hi World #zfill 自动补位 方法返回指定长度的字符串,原字符串右对齐,前面填充0 print ( 'lex li' .zfill( 10 )) #打印显示 0000lex li |