Python string processing function summary

Source: Internet
Author: User
Str & amp; #39; pythonStringfunction & amp; #39; generate the string variable str & amp; #39; pythonStringfunction & amp; #39; String length acquisition: len (str) str = 'Python String function'

Generate the String variable str = 'Python String function'

String length acquisition: len (str)

Example: print '% s length = % d' % (str, len (str ))

1. letter processing

All uppercase: str. upper ()

All lower case: str. lower ()

Case-insensitive: str. swapcase ()

Upper-case letters and lower-case letters: str. capitalize ()

Uppercase: str. title ()

Print '% s lower = % s' % (str, str. lower ())

Print '% s upper = % s' % (str, str. upper ())

Print '% s swapcase = % s' % (str, str. swapcase ())

Print '% s capitalize = % s' % (str, str. capitalize ())

Print '% s title = % s' % (str, str. title ())

2. formatting

Get fixed length, right alignment, left not enough space fill: str. ljust (width)

Get fixed length, left alignment, right side not enough space fill: str. ljust (width)

Get a fixed length and align in the middle. The two sides are not filled with spaces: str. ljust (width)

Get fixed length, right alignment

Print '% s ljust = % s' % (str, str. ljust (20 ))

Print '% s must UST = % s' % (str, str. must UST (20 ))

Print '% s center = % s' % (str, str. center (20 ))

Print '% s zfill = % s' % (str, str. zfill (20 ))


3. string search

Search for the specified string.-1: str. find ('t') is not returned ')

Search at the specified start position: str. find ('t', start)

Search by specified start and end locations: str. find ('t', start, end)

Search for str. rfind ('t') from the right ')

How many specified strings are searched: str. count ('t ')

All the methods above can be replaced by indexes. The difference is that if index is not found, an exception is thrown, while find returns-1.

Print '% s find nono = % d' % (str, str. find ('nono '))

Print '% s find t = % d' % (str, str. find ('t '))

Print '% s find t from % d = % d' % (str, 1, str. find ('t', 1 ))

Print '% s find t from % d to % d = % d' % (str, 1, 2, str. find ('t', 1, 2 ))

# Print '% s index nono' % (str, str. index ('nono', 1, 2 ))

Print '% s rfind t = % d' % (str, str. rfind ('t '))

Print '% s count t = % d' % (str, str. count ('t '))


4. string replacement

Replace old with new: str. replace ('old', 'new ')

Replace the old value of a specified number of times with new: str. replace ('old', 'new', maxReplaceTimes)

Print '% s replace t to * = % s' % (str, str. replace ('t ','*'))

Print '% s replace t to * = % s' % (str, str. replace ('t',' * ', 1 ))


5. remove spaces from strings and specify characters

Remove space on both sides: str. strip ()

Remove left space: str. lstrip ()

Right space: str. rstrip ()

Remove the strings on both sides: str. strip ('D'). the corresponding strings include lstrip and rstrip.

Str = 'Python String function'

Print '% s strip = % s' % (str, str. strip ())

Str = 'Python String function'

Print '% s strip = % s' % (str, str. strip ('D '))


6. split the string into arrays by specified characters: str. split ('')

Separated by spaces by default

Str = 'a B c de'

Print '% s strip = % s' % (str, str. split ())

Str = 'A-B-c-de'

Print '% s strip = % s' % (str, str. split ('-'))


7. string judgment

Start or not: str. startswith ('start ')

End with: str. endswith ('end ')

Whether all are letters or numbers: str. isalnum ()

Full letter: str. isalpha ()

Full number: str. isdigit ()

Lowercase?: str. islower ()

Uppercase or not: str. isupper ()

Str = 'Python String function'

Print '% s startwith t = % s' % (str, str. startswith ('t '))

Print '% s endwith d = % s' % (str, str. endswith ('D '))

Print '% s isalnum = % s' % (str, str. isalnum ())

Str = 'pythonstringfunction'

Print '% s isalnum = % s' % (str, str. isalnum ())

Print '% s isalpha = % s' % (str, str. isalpha ())

Print '% s isupper = % s' % (str, str. isupper ())

Print '% s islower = % s' % (str, str. islower ())

Print '% s isdigit = % s' % (str, str. isdigit ())

Str = '000000'

Print '% s isdigit = % s' % (str, str. isdigit ())

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.