Basic string manipulation
All standard sequence Operations (index, Shard, multiply, Judge membership, seek length, take maximum and minimum), also apply to strings. However, the string is immutable.
String method
There are too many ways to string, but here are just a few things that are particularly useful.
Find
The Find method can look up a substring in a longer string, which returns the leftmost index at the position where the substring is located. If not found, returns-1
>>> ' with a moo-moo here. and a Moo-moo there '. Find (' moo ')
7
>>> title= "Monty Python ' s Flying Circus"
>>> title.find (' Monty ')
0
>>> title.find (' Python ')
6
>>> title.find (' Flying ')
15
>>> title.find (' Zirquss ')
-1
This method can also accept the optional start and end points parameters.
>>> subject= ' $$$ Get rich now!!! $$$ '
>>> subject.find (' $$$ ')
0
>>> subject.find (' $$$ ', 1) #只提供起始点
20
>>> subject.find ('!!! ')
16
>>> subject.find ('!!! ', 0,16) #提供起始点和结束点
-1
Join
The Join method is a very important string method, which is the inverse method of the split method, which is used to add elements to the queue
>>> seq=[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
>>> sep= ' + '
>>> sep.join (seq)
' 1+2+3+4+5 '
>>> dirs= ', ' usr ', ' bin ', ' env '
>>> '/'. Join (dirs)
'/usr/bin/env '
>>> print ' C: ' + ' \ \ '. Join (dirs)
C:\usr\bin\env
Lower
Lower method returns the lowercase master of a string
>>> ' Trondheim Hammer Dance '. Lower ()
' Trondheim Hammer Dance '
This approach comes in handy if you want to write a ' case insensitive ' code-the code ignores the case state
>>> name= ' Gumby '
>>> names=[' Gumby ', ' Smith ', ' Jones '
>>> if Name.lower () in Names:print ' Found it! '
Found it!
Replace
The Replace method returns a string after all occurrences of a string have been replaced
>>> ' This was test '. Replace (' is ', ' EEZ ')
' Theez EEZ test '
Split
Split is a very important method, which is the inverse of join, used to split a string into a sequence.
>>> ' 1+2+3+4+5 '. Split (' + ')
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
>>> '/usr/bin/env '. Split ('/')
[', ' usr ', ' bin ', ' env ']
>>> ' Using The default '. Split ()
[' Using ', ' the ', ' Default ']
(If no delimiter is provided, the program will treat all spaces as delimiters)
Strip
The Strip method returns a string that strips both sides (excluding interior) spaces
>>> ' internal whitespace is kept '. Strip ()
' Internal whitespace is kept '
>>> ' ***spam*for*everyone!!! '. Strip (' *! ')
' Spam*for*everyone '
Translate
Translate, like the Replace method, can replace some parts of a string, except that translate can only replace a single character
The Maketrans function accepts two parameters: two equal-length strings, which indicate that the characters in the first string are replaced with characters from the same position in the second string
>>> from string import Maketrans
>>> intab= ' Aeiou '
>>> outab= ' 12345 '
>>> Trantab=maketrans (Intab,outab)
>>> str= ' This is String Example....wow '
>>> print str.translate (trantab)
Th3s 3s str3ng 2x1mpl2....w4w
Python learning Notes (String)