Home > Backend Development > Python Tutorial > Summary of python string processing functions

Summary of python string processing functions

高洛峰
Release: 2016-10-20 09:56:15
Original
1463 people have browsed it

str='python String function'

Generate string variable str='python String function'

Get the string length: len(str)

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

1. Letter processing

All uppercase: str.upper()

All lowercase: str.lower()

Case swap: str.swapcase()

The first letter is capitalized, the rest is lowercase :str.capitalize()

Capitalize the first letter: 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 related

Get a fixed length, right-aligned, and fill in spaces if there is not enough space on the left: str.ljust(width )

Get a fixed length, aligned to the left, and fill in spaces if the right side is not enough: str.ljust(width)

Get a fixed length, aligned in the middle, fill in spaces if there are not enough spaces on both sides: str.ljust(width)

Get a fixed length , right-aligned, fill in the left part with 0s

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

print '%s rjust=%s' % (str,str.rjust (20))

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

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


3. String search related

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

Specify the starting position to search: str.find('t',start)

Specify Start and end position search: str.find('t',start,end)

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

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

All the above methods can be replaced by index. The difference is that if index is not found, an exception will be thrown, and 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 related

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

Replace the specified number of times old 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 and specified characters from a string

Remove spaces on both sides: str.strip()

Remove spaces on the left: str.lstrip()

Remove spaces on the right: str.rstrip()

Remove strings on both sides: str.strip('d'), corresponding to lstrip, 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 an array by specified characters: str.split(' ' )

Default separated by spaces

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 related

Whether it starts with start: str.startswith('start')

Whether it starts with end Ending: str.endswith('end')

Whether it is all letters or numbers: str.isalnum()

Whether it is all letters: str.isalpha()

Whether it is all numbers: str.isdigit()

Whether it is all Lowercase: str.islower()

Whether all uppercase: 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='3423'

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template