整理了python的str的各种methods.
str.capitalize()
Return a copy of the string with its first character capitalized and the rest lowercased.
将入参的第一个字符大写,但是不修改入参。
1 | In [15]: s = 'hello, world' |
str.center(width[, fillchar])
Return centered in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).
- 返回一个给定长度的字符串,将原始字符串居中
- 如果width小于原始字符的长度,那么返回原始字符
- 默认用ASCII的空格填充,也可以设置填充字符
1 | In [19]: s.center(3) |
str.count(sub[, start[, end]])
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
- 返回子字符串sub在[start, end]中出现的次数(不重叠)
- 为什么下面那个例子
sc.count('')
会返回71呢
1 | In [26]: sc |
str.encode(encoding=”utf-8”, errors=”strict”)
Return an encoded version of the string as a bytes object. Default encoding is ‘utf-8’. errors may be given to set a different error handling scheme. The default for errors is ‘strict’, meaning that encoding errors raise a UnicodeError. Other possible values are ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ and any other name registered via codecs.register_error(), see section Error Handlers. For a list of possible encodings, see section Standard Encodings.
str.endswith(suffix[, start[, end]])
Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.
- 若str以suffix结尾返回
True
,否则返回False
- suffix可以是一个元组(list)
- 可选开始、结尾位置
1 | In [32]: s |
str.expandtabs(tabsize=8)
Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every tabsize characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab (\t), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline (\n) or return (\r), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed.
将str中的tab用空格替换
str.find(sub[, start[, end]])
Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.
- 返回str中首次出现sub的位置
- start和end可选
- 如果sub没有出现则返回-1
1 | In [41]: s |
该函数应当仅用于寻找sub在str中的位置,如果想要确定sub是否是str的子串,应该用in
1 | In [44]: 'ld' in s |
str.format()
Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.
1 | "The sum of 1 + 2 is {0}".format(1+2) |
str.index(sub[, start[, end]])
Like find(), but raise ValueError when the substring is not found.
像find
,但是sub不存在时,会raise ValueError
str.isalnum()
Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. A character c is alphanumeric if one of the following returns True: c.isalpha(), c.isdecimal(), c.isdigit(), or c.isnumeric().
- 返回
True
的情形:str仅包含字母或数字,且至少要有一个 - 否则返回
False
1 | In [45]: s |
str.isalpha()
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard.
- 至少一个字符,且所有的字符为字母,则返回
True
- 否则返回
False
str.isascii()
Return true if the string is empty or all characters in the string are ASCII, false otherwise. ASCII characters have code points in the range U+0000-U+007F.
- 至少一个字符,且所有的字符为ASCII字母,则返回
True
- 否则返回
False
str.isdecimal()
Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.
‘count’,
‘decode’,
‘encode’,
‘endswith’,
‘expandtabs’,
‘find’,
‘format’,
‘index’,
‘isalnum’,
‘isalpha’,
‘isdigit’,
‘islower’,
‘isspace’,
‘istitle’,
‘isupper’,
‘join’,
‘ljust’,
‘lower’,
‘lstrip’,
‘partition’,
‘replace’,
‘rfind’,
‘rindex’,
‘rjust’,
‘rpartition’,
‘rsplit’,
‘rstrip’,
‘split’,
‘splitlines’,
‘startswith’,
‘strip’,
‘swapcase’,
‘title’,
‘translate’,
‘upper’,
‘zfill’