字符串:
赋值方法
a = ‘name‘
a = str(‘name‘)
字符串的方法:
1 #!/usr/bin/env python 2 class str(object): 3 """ 4 str(object=‘‘) -> str 5 str(bytes_or_buffer[, encoding[, errors]]) -> str 6 7 Create a new string object from the given object. If encoding or 8 errors is specified, then the object must expose a data buffer 9 that will be decoded using the given encoding and error handler. 10 Otherwise, returns the result of object.__str__() (if defined) 11 or repr(object). 12 encoding defaults to sys.getdefaultencoding(). 13 errors defaults to ‘strict‘. 14 """ 15 def capitalize(self): # real signature unknown; restored from __doc__ 16 ‘‘‘首字母大写‘‘‘ 17 """ 18 S.capitalize() -> str 19 20 Return a capitalized version of S, i.e. make the first character 21 have upper case and the rest lower case. 22 """ 23 return "" 24 25 def casefold(self): # real signature unknown; restored from __doc__ 26 ‘‘‘全部转换为小写‘‘‘ 27 """ 28 S.casefold() -> str 29 30 Return a version of S suitable for caseless comparisons. 31 """ 32 return "" 33 34 def center(self, width, fillchar=None): # real signature unknown; restored from __doc__ 35 ‘‘‘内容居中,width:总长度,fillchar:空白处填充内容,默认无‘‘‘ 36 """ 37 S.center(width[, fillchar]) -> str 38 39 Return S centered in a string of length width. Padding is 40 done using the specified fill character (default is a space) 41 """ 42 return "" 43 44 def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 45 ‘‘‘子序列个数‘‘‘ 46 """ 47 S.count(sub[, start[, end]]) -> int 48 49 Return the number of non-overlapping occurrences of substring sub in 50 string S[start:end]. Optional arguments start and end are 51 interpreted as in slice notation. 52 """ 53 return 0 54 55 def encode(self, encoding=‘utf-8‘, errors=‘strict‘): # real signature unknown; restored from __doc__ 56 ‘‘‘编码,针对unicode‘‘‘ 57 """ 58 S.encode(encoding=‘utf-8‘, errors=‘strict‘) -> bytes 59 60 Encode S using the codec registered for encoding. Default encoding 61 is ‘utf-8‘. errors may be given to set a different error 62 handling scheme. Default is ‘strict‘ meaning that encoding errors raise 63 a UnicodeEncodeError. Other possible values are ‘ignore‘, ‘replace‘ and 64 ‘xmlcharrefreplace‘ as well as any other name registered with 65 codecs.register_error that can handle UnicodeEncodeErrors. 66 """ 67 return b"" 68 69 def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__ 70 ‘‘‘判断是否以特定值结尾,也可以是一个元组。如果是则为真(True)‘‘‘ 71 """ 72 S.endswith(suffix[, start[, end]]) -> bool 73 74 Return True if S ends with the specified suffix, False otherwise. 75 With optional start, test S beginning at that position. 76 With optional end, stop comparing S at that position. 77 suffix can also be a tuple of strings to try. 78 """ 79 return False 80 81 def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__ 82 ‘‘‘将一个tab键转换为空格,默认为8个空格‘‘‘ 83 """ 84 S.expandtabs(tabsize=8) -> str 85 86 Return a copy of S where all tab characters are expanded using spaces. 87 If tabsize is not given, a tab size of 8 characters is assumed. 88 """ 89 return "" 90 91 def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 92 ‘‘‘寻找子序列的位置,如果没有找到则返回 -1,只查找一次找到及返回 ‘‘‘ 93 """ 94 S.find(sub[, start[, end]]) -> int 95 96 Return the lowest index in S where substring sub is found, 97 such that sub is contained within S[start:end]. Optional 98 arguments start and end are interpreted as in slice notation. 99 100 Return -1 on failure. 101 """ 102 return 0 103 104 def format(*args, **kwargs): # known special case of str.format 105 ‘‘‘字符串格式转化‘‘‘ 106 """ 107 S.format(*args, **kwargs) -> str 108 109 Return a formatted version of S, using substitutions from args and kwargs. 110 The substitutions are identified by braces (‘{‘ and ‘}‘). 111 """ 112 pass 113 114 def format_map(self, mapping): # real signature unknown; restored from __doc__ 115 """ 116 S.format_map(mapping) -> str 117 118 Return a formatted version of S, using substitutions from mapping. 119 The substitutions are identified by braces (‘{‘ and ‘}‘). 120 """ 121 return "" 122 123 def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 124 ‘‘‘子序列的位置,如果没有找到返回错误‘‘‘ 125 """ 126 S.index(sub[, start[, end]]) -> int 127 128 Like S.find() but raise ValueError when the substring is not found. 129 """ 130 return 0 131 132 def isalnum(self): # real signature unknown; restored from __doc__ 133 ‘‘‘是否是字母或数字‘‘‘ 134 """ 135 S.isalnum() -> bool 136 137 Return True if all characters in S are alphanumeric 138 and there is at least one character in S, False otherwise. 139 """ 140 return False 141 142 def isalpha(self): # real signature unknown; restored from __doc__ 143 ‘‘‘是否所有字符都为字母‘‘‘ 144 """ 145 S.isalpha() -> bool 146 147 Return True if all characters in S are alphabetic 148 and there is at least one character in S, False otherwise. 149 """ 150 return False 151 152 def isdecimal(self): # real signature unknown; restored from __doc__ 153 ‘‘‘是否为十进制数‘‘‘ 154 """ 155 S.isdecimal() -> bool 156 157 Return True if there are only decimal characters in S, 158 False otherwise. 159 """ 160 return False 161 162 def isdigit(self): # real signature unknown; restored from __doc__ 163 ‘‘‘是否所有字符都为数字‘‘‘ 164 """ 165 S.isdigit() -> bool 166 167 Return True if all characters in S are digits 168 and there is at least one character in S, False otherwise. 169 """ 170 return False 171 172 def isidentifier(self): # real signature unknown; restored from __doc__ 173 ‘‘‘是否为有效标识符‘‘‘ 174 """ 175 S.isidentifier() -> bool 176 177 Return True if S is a valid identifier according 178 to the language definition. 179 180 Use keyword.iskeyword() to test for reserved identifiers 181 such as "def" and "class". 182 """ 183 return False 184 185 def islower(self): # real signature unknown; restored from __doc__ 186 ‘‘‘是否所有字符都为小写‘‘‘ 187 """ 188 S.islower() -> bool 189 190 Return True if all cased characters in S are lowercase and there is 191 at least one cased character in S, False otherwise. 192 """ 193 return False 194 195 def isnumeric(self): # real signature unknown; restored from __doc__ 196 ‘‘‘是否只有数字‘‘‘ 197 """ 198 S.isnumeric() -> bool 199 200 Return True if there are only numeric characters in S, 201 False otherwise. 202 """ 203 return False 204 205 def isprintable(self): # real signature unknown; restored from __doc__ 206 ‘‘‘内容是否都是可见的字符,不可见的包括tab键,换行符。空格为可见字符‘‘‘ 207 """ 208 S.isprintable() -> bool 209 210 Return True if all characters in S are considered 211 printable in repr() or S is empty, False otherwise. 212 """ 213 return False 214 215 def isspace(self): # real signature unknown; restored from __doc__ 216 ‘‘‘是否只有空格组成‘‘‘ 217 """ 218 S.isspace() -> bool 219 220 Return True if all characters in S are whitespace 221 and there is at least one character in S, False otherwise. 222 """ 223 return False 224 225 def istitle(self): # real signature unknown; restored from __doc__ 226 ‘‘‘是否为标题化的‘‘‘ 227 """ 228 S.istitle() -> bool 229 230 Return True if S is a titlecased string and there is at least one 231 character in S, i.e. upper- and titlecase characters may only 232 follow uncased characters and lowercase characters only cased ones. 233 Return False otherwise. 234 """ 235 return False 236 237 def isupper(self): # real signature unknown; restored from __doc__ 238 ‘‘‘是否都为大写字母‘‘‘ 239 """ 240 S.isupper() -> bool 241 242 Return True if all cased characters in S are uppercase and there is 243 at least one cased character in S, False otherwise. 244 """ 245 return False 246 247 def join(self, iterable): # real signature unknown; restored from __doc__ 248 ‘‘‘连接,以S作为分隔符,把所有iterable中的元素合并成一个新的字符串‘‘‘ 249 """ 250 S.join(iterable) -> str 251 252 Return a string which is the concatenation of the strings in the 253 iterable. The separator between elements is S. 254 """ 255 return "" 256 257 def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__ 258 ‘‘‘左对齐,width 字符串的长度,右侧以fillchar填充,默认为空‘‘‘ 259 """ 260 S.ljust(width[, fillchar]) -> str 261 262 Return S left-justified in a Unicode string of length width. Padding is 263 done using the specified fill character (default is a space). 264 """ 265 return "" 266 267 def lower(self): # real signature unknown; restored from __doc__ 268 ‘‘‘所有字母大写变小写,返回一个小写的副本‘‘‘ 269 """ 270 S.lower() -> str 271 272 Return a copy of the string S converted to lowercase. 273 """ 274 return "" 275 276 def lstrip(self, chars=None): # real signature unknown; restored from __doc__ 277 ‘‘‘去除左侧空白‘‘‘ 278 """ 279 S.lstrip([chars]) -> str 280 281 Return a copy of the string S with leading whitespace removed. 282 If chars is given and not None, remove characters in chars instead. 283 """ 284 return "" 285 286 def maketrans(self, *args, **kwargs): # real signature unknown 287 """ 288 Return a translation table usable for str.translate(). 289 290 If there is only one argument, it must be a dictionary mapping Unicode 291 ordinals (integers) or characters to Unicode ordinals, strings or None. 292 Character keys will be then converted to ordinals. 293 If there are two arguments, they must be strings of equal length, and 294 in the resulting dictionary, each character in x will be mapped to the 295 character at the same position in y. If there is a third argument, it 296 must be a string, whose characters will be mapped to None in the result. 297 """ 298 pass 299 300 def partition(self, sep): # real signature unknown; restored from __doc__ 301 ‘‘‘分割,把字符以sep分割为 前 中 后 三个部分‘‘‘ 302 """ 303 S.partition(sep) -> (head, sep, tail) 304 305 Search for the separator sep in S, and return the part before it, 306 the separator itself, and the part after it. If the separator is not 307 found, return S and two empty strings. 308 """ 309 pass 310 311 def replace(self, old, new, count=None): # real signature unknown; restored from __doc__ 312 ‘‘‘替换,old替换为new,可以指定次数(count)‘‘‘ 313 """ 314 S.replace(old, new[, count]) -> str 315 316 Return a copy of S with all occurrences of substring 317 old replaced by new. If the optional argument count is 318 given, only the first count occurrences are replaced. 319 """ 320 return "" 321 ########################################################### 322 #所有以r开头和上面一样的方法,都和上面反方向(即从右到左)# 323 ########################################################### 324 def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 325 """ 326 S.rfind(sub[, start[, end]]) -> int 327 328 Return the highest index in S where substring sub is found, 329 such that sub is contained within S[start:end]. Optional 330 arguments start and end are interpreted as in slice notation. 331 332 Return -1 on failure. 333 """ 334 return 0 335 336 def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ 337 """ 338 S.rindex(sub[, start[, end]]) -> int 339 340 Like S.rfind() but raise ValueError when the substring is not found. 341 """ 342 return 0 343 344 def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__ 345 """ 346 S.rjust(width[, fillchar]) -> str 347 348 Return S right-justified in a string of length width. Padding is 349 done using the specified fill character (default is a space). 350 """ 351 return "" 352 353 def rpartition(self, sep): # real signature unknown; restored from __doc__ 354 """ 355 S.rpartition(sep) -> (head, sep, tail) 356 357 Search for the separator sep in S, starting at the end of S, and return 358 the part before it, the separator itself, and the part after it. If the 359 separator is not found, return two empty strings and S. 360 """ 361 pass 362 363 def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__ 364 """ 365 S.rsplit(sep=None, maxsplit=-1) -> list of strings 366 367 Return a list of the words in S, using sep as the 368 delimiter string, starting at the end of the string and 369 working to the front. If maxsplit is given, at most maxsplit 370 splits are done. If sep is not specified, any whitespace string 371 is a separator. 372 """ 373 return [] 374 375 def rstrip(self, chars=None): # real signature unknown; restored from __doc__ 376 """ 377 S.rstrip([chars]) -> str 378 379 Return a copy of the string S with trailing whitespace removed. 380 If chars is given and not None, remove characters in chars instead. 381 """ 382 return "" 383 384 def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__ 385 ‘‘‘分割,以什么分割,maxsplit分割次数‘‘‘ 386 """ 387 S.split(sep=None, maxsplit=-1) -> list of strings 388 389 Return a list of the words in S, using sep as the 390 delimiter string. If maxsplit is given, at most maxsplit 391 splits are done. If sep is not specified or is None, any 392 whitespace string is a separator and empty strings are 393 removed from the result. 394 """ 395 return [] 396 397 def splitlines(self, keepends=None): # real signature unknown; restored from __doc__ 398 ‘‘‘根据换行符分割‘‘‘ 399 """ 400 S.splitlines([keepends]) -> list of strings 401 402 Return a list of the lines in S, breaking at line boundaries. 403 Line breaks are not included in the resulting list unless keepends 404 is given and true. 405 """ 406 return [] 407 408 def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__ 409 ‘‘‘是否以self起始,可以指定开始和结束位置‘‘‘ 410 """ 411 S.startswith(prefix[, start[, end]]) -> bool 412 413 Return True if S starts with the specified prefix, False otherwise. 414 With optional start, test S beginning at that position. 415 With optional end, stop comparing S at that position. 416 prefix can also be a tuple of strings to try. 417 """ 418 return False 419 420 def strip(self, chars=None): # real signature unknown; restored from __doc__ 421 ‘‘‘移除两边空白‘‘‘ 422 """ 423 S.strip([chars]) -> str 424 425 Return a copy of the string S with leading and trailing 426 whitespace removed. 427 If chars is given and not None, remove characters in chars instead. 428 """ 429 return "" 430 431 def swapcase(self): # real signature unknown; restored from __doc__ 432 ‘‘‘大小写反转‘‘‘ 433 """ 434 S.swapcase() -> str 435 436 Return a copy of S with uppercase characters converted to lowercase 437 and vice versa. 438 """ 439 return "" 440 441 def title(self): # real signature unknown; restored from __doc__ 442 ‘‘‘标题化,即首字母大写其余字母为小写‘‘‘ 443 """ 444 S.title() -> str 445 446 Return a titlecased version of S, i.e. words start with title case 447 characters, all remaining cased characters have lower case. 448 """ 449 return "" 450 451 def translate(self, table): # real signature unknown; restored from __doc__ 452 ‘‘‘转换,需要做一个对应表,删除的放到最后面‘‘‘ 453 """ 454 S.translate(table) -> str 455 456 Return a copy of the string S in which each character has been mapped 457 through the given translation table. The table must implement 458 lookup/indexing via __getitem__, for instance a dictionary or list, 459 mapping Unicode ordinals to Unicode ordinals, strings, or None. If 460 this operation raises LookupError, the character is left untouched. 461 Characters mapped to None are deleted. 462 """ 463 return "" 464 465 def upper(self): # real signature unknown; restored from __doc__ 466 ‘‘‘小写字母转换为大写‘‘‘ 467 """ 468 S.upper() -> str 469 470 Return a copy of S converted to uppercase. 471 """ 472 return "" 473 474 def zfill(self, width): # real signature unknown; restored from __doc__ 475 ‘‘‘返回width长度的字符串,原字符串右对齐,前面填充 0 ‘‘‘ 476 """ 477 S.zfill(width) -> str 478 479 Pad a numeric string S with zeros on the left, to fill a field 480 of the specified width. The string S is never truncated. 481 """ 482 return ""
str
示例:
####
capitalize()
>>> name = ‘binges wang‘
>>> name.capitalize()
‘Binges wang‘
####
casefold()
>>> name = ‘Binges Wang‘
>>> name.casefold()
‘binges wang‘
####
center()
>>> name = ‘binges‘
>>> name.center(10)
‘ binges ‘
>>> name.center(10,‘#‘)
‘##binges##‘
####
endswith()
>>> name = ‘binges‘
>>> name.endswith(‘s‘)
True
>>> name.endswith(‘es‘)
True
>>> name.endswith(‘ed‘)
False
####
find()
>>> name = ‘binges wang‘
>>> name.find(‘s‘)
5
>>> name.find(‘x‘)
-1
>>> name.find(‘g‘)
3
>>> name.find(‘an‘)
8
####
index()
>>> name = ‘binges wang‘
>>> name.index(‘a‘)
8
>>> name.index(‘s‘)
5
>>> name.index(‘an‘)
8
>>> name.index(‘x‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
####
isalnum()
>>> name = ‘binges‘
>>> name.isalnum()
True
>>> name = ‘binges 12‘
>>> name.isalnum()
False
>>> name = ‘binges12‘
>>> name.isalnum()
True
>>> age = ‘12‘
>>> age.isalnum()
True
####
isalpha()
>>> name = ‘binges12‘
>>> name.isalpha()
False
>>> name = ‘binges wang‘
>>> name.isalpha()
False
>>> name = ‘binges‘
>>> name.isalpha()
True
####
isdigit()
>>> age = ‘23‘
>>> age.isdigit()
True
>>> age = ‘23 ‘
>>> age.isdigit()
False
>>> age = ‘23.5‘
>>> age.isdigit()
False
####
isidentifier()
>>> name = ‘binges wang‘
>>> name.isidentifier()
False
>>> name = ‘binges‘
>>> name.isidentifier()
True
####
islower()
>>> name = ‘binges‘
>>> name.islower()
True
>>> name = ‘binges wang‘
>>> name.islower()
True
>>> name = ‘binges 12‘
>>> name.islower()
True
>>> name = ‘binges Wang‘
>>> name.islower()
False
####
isprintable()
>>> name = ‘binges wang‘
>>> name.isprintable()
False
>>> name = ‘binges wang‘
>>> name.isprintable()
True
>>> name = ‘binges\nwang‘
>>> name.isprintable()
False
####
isspace()
>>> name = ‘ ‘ #两个空格
>>> name.isspace()
True
>>> name = ‘ ‘ #一个tab键
>>> name.isspace()
True
>>> name = ‘bings wang‘
>>> name.isspace()
False
####
istitle()
>>> name = ‘binges wang‘
>>> name.istitle()
False
>>> name = ‘Binges wang‘
>>> name.istitle()
False
>>> name = ‘Binges Wang‘
>>> name.istitle()
True
####
isupper()
>>> name = ‘BINGEs‘
>>> name.isupper()
False
>>> name = ‘BINGES‘
>>> name.isupper()
True
>>> name = ‘BINGES WANG‘
>>> name.isupper()
True
####
join()
>>> a = ‘ ‘ #一个空格
>>> a.join(b)
‘b i n g e s‘
####
ljust()
>>> name = ‘binges‘
>>> name.ljust(10)
‘binges ‘
>>> name.ljust(10,‘&‘)
‘binges&&&&‘
####
lower()
>>> name = ‘BinGes WanG‘
>>> name.lower()
‘binges wang‘
>>> name
‘BinGes WanG‘
####
lstrip()
>>> name = ‘ binges wang‘ #一个空格和一个tab键
>>> name.lstrip()
‘binges wang‘
####
partition(‘n‘)
>>> name = ‘binges wang‘
>>> name.partition(‘n‘)
(‘bi‘, ‘n‘, ‘ges wang‘)
####
replace()
>>> name = ‘binges wang‘
>>> name.replace(‘n‘,‘w‘)
‘biwges wawg‘
>>> name
‘binges wang‘
>>> name.replace(‘n‘,‘w‘,1)
‘biwges wang‘
####
startswith()
>>> name = ‘binges‘
>>> name.startswith(‘b‘)
True
>>> name.startswith(‘bd‘)
False
>>> name.startswith(‘n‘,1,5)
False
>>> name.startswith(‘n‘,2,5)
True
####
split()
>>> name = ‘binges‘
>>> name.split(‘n‘)
[‘bi‘, ‘ges‘]
>>> name = ‘binges wang‘
>>> name.split(‘n‘)
[‘bi‘, ‘ges wa‘, ‘g‘]
>>> name.split(‘n‘,1)
[‘bi‘, ‘ges wang‘]
####
strip()
>>> name = ‘ binges wang ‘ #空白处都为一个空格和一个tab键
>>> name.strip()
‘binges \twang‘
####
swapcase()
>>> name = ‘BinGes WanG‘
>>> name.swapcase()
‘bINgES wANg‘
####
title()
>>> name = ‘BinGes WanG‘
>>> name.title()
‘Binges Wang‘
####
upper()
>>> name
‘BinGes WanG‘
>>> name.upper()
‘BINGES WANG‘
####
zfill(10)
>>> name = ‘binges‘
>>> name.zfill(10)
‘0000binges‘
####