A concise summary of the Python string handling function

  • 2020-05-09 18:44:55
  • OfStack

Returns a string that has been stripped of the specified character

White space characters are removed by default

Delete the first and last characters: str.strip ([char])
Delete first character: str.lstrip ([char])
Delete the last character str.strip ([char])

Determines if the first and last characters match

A successful match returns True, otherwise False
Match the first character: str.startswith (char[, start[, end]])
Match last character: str.endswith (char[, start[, end]])

Find the character, find the return character location, otherwise return -1

Find str.find (char[, start[, end]]) from the beginning of the string
Find str. find(char[, start[, end]]) from the end of the string

Decomposition characters

str.split([sep [, maxsplit]])

Connect the character

str.join(iterable)
"str" is the concatenation character, and "iterable" is the iterated string to be concatenated

Substitution characters

str.replace(old, new [,count])
Returns the replaced string,count identifies the number of times replaced, and all characters are replaced by default

Break the string by newline, and return the list of lines and columns

str.splitlines([keepends])
The result does not retain the newline character unless keepends is True.

Case conversion

Case interchange: str.swapace ()
Convert to uppercase: str.upper ()
Convert to lowercase: str.lower ()


Related articles: