A brief analysis of the usage of python's built in string handling function

  • 2020-04-02 13:42:38
  • OfStack

1. Lower () : convert all capital letters into lowercase letters. Such as:


name='G'
b=name.lower()

2. "title ": converts the string to a title, that is, capitalizes the first letter of all words and lowercase the rest. Use the same method as lower()

Replace: returns a string in which all matches have been replaced.


'This is a test'.replace('is','are')

Four, split: split the string into sequences


'1+2+3+4+5'.split('+')

The default program USES all Spaces as delimiters.

Strip: returns a string that removes Spaces on both sides (not including the inside)


'               in wh is kepy         '.strip()

Translate: replace some parts of the string. Translate knowledge handles single characters. But you can do multiple substitutions at once, multiple substitutions means you can replace a with b, c with d, and replace can only replace a with b at a time. The use of translate is more complicated. If you need to use it, please refer to the documentation that specifically describes it.


Related articles: