Python built-in string processing functions (covering daily use), python built-in

Source: Internet
Author: User

Python built-in string processing functions (covering daily use), python built-in

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 ))

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 ())
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 ))

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 '))

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 ))

Character string with spaces and specified 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 '))

Splits string by specified character into an array: 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 ('-'))

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 ())


Python string processing problem: how to splice the Arc before several strings and the content in it to the end of the string?

#! Python3.2

# Define conversion functions
Def convert (s ):
Index1 = s. index ('(')
Index2 = s. index (')')
Index3 = s. index ('. ')
Return s [index2 + 1: index3] + s [: index2 + 1] + s [index3:]

# Enter your own list ls
Ls = []

# Start Conversion
For s in ls:
Print (convert (s ))

Python handles string processing issues in files:

The answer for the upstairs is changed to the first line.
F1 = open('1.txt ')
No other problems

However, there is a simpler way:

F1 = open('1.txt ')

Linen = 1

For line in reader:
If not line. find ('44. 2% ') =-1:
Print linen
Linen + = 1

F1.close ()

If it is helpful to you, please remember to adopt a satisfactory answer. Thank you! Wish you a happy life!
 

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.