Explanation of common string functions in Python, and explanation of python Functions

Source: Internet
Author: User

Explanation of common string functions in Python, and explanation of python Functions

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 with you. I hope to give you a reference and support for the help house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.