Common methods of string processing

method Description
capitalize() Capitalize the first letter of the string, and do not deal with it if it does not start with a letter
lower() Convert the letters of the string to lowercase
upper() () Convert the letters of the string to uppercase
center(width[, fillbyte]) Return a string with a length of width, if the length is not enough, the entire string will be returned, if the length is not enough, fill with fillbyte on both sides
encode(encoding=“utf-8”, errors=“strict”) Returns the version of the original string encoded as a byte string object. The default encoding is'utf-8'. You can give errors to set different error handling schemes. The default value of errors is'strict', which means that encoding errors will cause UnicodeError.
endswith(suffix[, start[, end]) Returns True if the string ends with the specified suffix, otherwise returns False. suffix can also be a tuple composed of multiple suffixes for search. If there is an optional start option, the check will start from the specified position. If there is an option end, the comparison will stop at the specified position.
find(sub[, start[, end]]) Returns the minimum index of the substring sub found in the s[start:end] slice. The optional parameters start and end will be interpreted as slice notation. If sub is not found, -1 is returned.
rfind(sub[, start[, end]]) Returns the largest (rightmost) index of the substring sub found in the string, so that sub will be contained in s[start:end]. The optional parameters start and end will be interpreted as slice notation. If not found, it returns -1.
isdigit() Judge whether the string is entirely composed of numbers, if it is True, otherwise it is False
isspace() Determine whether the string is composed of all characters, if it is True, otherwise False
isalpha () Judge whether the string is composed entirely of spaces, if it is True, otherwise False
count(sub[, start[, end]]) Return the number of non-overlapping occurrences of the substring sub in the range [start, end]. The optional parameters start and end will be interpreted as slice notation.
join() Generally used as a connection list, connect the list with a string as an interval
strip() Return a copy of the original string, removing the leading and ending characters. By default, spaces are removed.
lstrip() Return a copy of the original string, removing the leading characters. By default, spaces are removed.
rstrip() Return a copy of the original string, removing the last character. By default, spaces are removed.
split(sep=None, maxsplit=-1) Returns a list of words in the string, using sep as the delimiting string. If maxsplit is given, at most maxsplit splits will be performed (therefore, the list will have at most maxsplit+1 elements). If maxsplit is not specified or is -1, the number of splits is not limited (all possible splits are performed).
If sep is given, consecutive separators will not be combined together but treated as separate empty strings (for example, '1,2'.split(',') will return ['1','', '2']). The sep parameter may consist of multiple characters (for example, '1<>2<>3'.split('<>') will return ['1', '2', '3']). Splitting an empty string with the specified separator will return [''].
replace(old, new[, count]) Returns a copy of the string, in which all occurrences of the substring old will be replaced with new. If the optional parameter count is given, only the first count occurrences will be replaced.
[l, r, sep] Generally used to display string fragments and output in reverse order,
l is the starting position, r is the end point, and sep is the step length

Example:

str_test = 'hello world'
print(str_test.capitalize())  # 首字母大写

str_test = 'heLLo world'
print(str_test.lower())  # casefold()差不多效果,将所有字符串转换为小写,中文字符不会管
print(str_test.center(20, '-'))
print(str_test.count('o'))
print(str_test.encode())
print(str_test.endswith('d'))
print(str_test.find('o'))
print(str_test.isdigit())
print(str_test.isalpha())
print(str_test.isspace())
print(str_test.upper())
a = '-'
b = ['mmm', 'bbb', 'vvv']
print(a.join(b))
print(str_test.strip('h'))
print(str_test.lstrip('h'))
print(str_test.rstrip('d'))
print(str_test.rfind('o'))
print(str_test.split())
print(str_test.replace('o', 'a'))
print(str_test[::-1])  # 倒序输出

result:

Hello world
hello world
----heLLo world-----
2
b'heLLo world'
True
4
False
False
False
HELLO WORLD
mmm-bbb-vvv
eLLo world
eLLo world
heLLo worl
7
['heLLo', 'world']
heLLa warld
dlrow oLLeh

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/plan_jok/article/details/110944187