The following is a detailed explanation of the commonly used string functions in Python. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.
Case-insensitive conversion of characters in a string
1. str. lower () // lower case
>>> 'Skate'. lower ()
'Skate'
2. str. upper () // uppercase
>>> 'Skate'. upper ()
'Skate'
3. str. swapcase () // case-insensitive swap
>>> 'Skate'. swapcase ()
'Skate'
4. str. title () // upper-case letters and lower-case letters
>>> 'Skate'. title ()
'Skate'
Alignment of string in output
1. str. ljust (width, [fillchar]) // output width characters. str is left aligned. fill in the missing parts with fillchar. the default value is space.
>>> 'Skate'. ljust (10)
'Skate'
>>> 'Skate'. ljust (10, '0 ')
'Skate00000'
2. str. fill UST (width, [fillchar]) // output width characters, right alignment of str. fill the remaining parts with fillchar. the default value is space.
>>> 'Skate'. must UST (10, '0 ')
'000skate'
>>> 'Skate'. must UST (10)
'Skate'
3. str. center (width, [fillchar]) // center alignment
>>> 'Skate'. center (10)
'Skate'
>>> 'Skate'. center (10, '0 ')
'00skate000'
4. str. zfill (width) // change str to width length and align it on the right. fill the remaining part with 0.
>>> 'Skate'. zfill (10)
'000skate'
String search
Str. find ('t') // search for the specified string.-1 is not returned.
Str. find ('t', start) // specify the start position for search
Str. find ('t', start, end) // you can specify the start and end positions for a search:
Str. rfind ('t') // search from the right
Str. count ('t') // How many specified strings are searched:
Eg:
>>> 'Skate'. find ('t ')
3
>>> 'Skate'. find ('t', 2)
3
>>> 'Skate'. find ('t', 2, 4)
3
>>> 'Skate'. rfind ('t ')
3
>>> 'Skate'. count ('t ')
1
String replacement
Str. replace ('old', 'new') // replace old with new
Str. replace ('old', 'new', maxReplaceTimes) // replace the specified number of old with new.
Eg:
>>> 'Skatekate '. replace ('s','s ')
'Skatesket'
>>> 'Skatekate '. replace ('s','s ', 1)
'Skatesket'
>>>
Character string with spaces and specified characters
Str. strip ([chars]) // remove the chars on both sides. the default value is space.
Str. lstrip ([chars]) // go to chars on the left. the default value is space.
Str. rstrip ([chars]) // go to chars on the right. the default value is space.
String segmentation
Str. split ([sep, [maxsplit]) // use the sep separator to split str into a list. Maxsplit indicates the number of splits. The default delimiter is a blank character.
Str. rsplit ([sep, [maxsplit])
Str. splitlines ([keepends]) // divides str into a list based on the row delimiter. keepends is a bool value. if it is true, the row delimiter is retained.
Eg:
>>> 'Skateskate '. split ()
['Skatesket']
>>> 'Skatekate '. split ('e ')
['Skat', 'skat', '']
>>> 'Skate skate'. rsplit ('')
['Skate', 'skate']
>>> 'Skate \ n skate1 '. splitlines ()
['Skate', 'skate1']
>>> 'Skate \ n skate1 '. splitlines (1)
['Skate \ n', 'skate1']
>>>
String connection
Str. join (seq) // concatenates the sequence represented by seq (string sequence) with str
Eg:
>>> 'Skate'. join ('20140901 ')
'1skate1skate1
String judgment
Str. startwith (prefix [, start [, end]) // whether to start with prefix
Str. endwith (suffix [, start [, end]) // determine whether to end with suffix
Str. isalnum () // whether it is all letters and numbers with at least one character
Str. isalpha () // whether it is all letters with at least one character
Str. isdigit () // whether it is all numbers with at least one character
Str. isspace () // whether all are blank characters with at least one character
Str. islower () // whether the letters in str are all lowercase letters
Str. isupper () // specifies whether the letters in str are uppercase letters.
Str. istitle () // whether str is capitalized
Eg:
>>> 'Skate'. startswith ('s ')
True
>>> 'Skate'. startswith ('s ', 1, 2)
False
>>> 'Skate'. endswith ('s ', 1, 2)
False
>>> 'Skate'. endswith ('e', 1, 2)
False
>>> 'Skate'. endswith ('e', 1)
True
>>>
True
>>> 'Skate'. isalnum ()
True
>>> '200'. isalnum ()
True
>>> 'Skate222 '. isalnum ()
True
>>> 'Skate 222 '. isalnum ()
False
>>>
The above explanation of the commonly used string functions in Python is a small part of the content shared to everyone. I hope to give you a reference and support for PHP.
For more articles about commonly used string functions in Python, refer to the Chinese PHP website!