A common method of python string

1, join method: string concatenation -------> str 
A = "You are the wind I am the sand"
. B = "@" the Join (A)
Print (b)
>>> are you @ @ wind I @ @ child @ @ sand is

2, strip method: processing about space, \ t, \ n -----> str
  can also handle specified content a.strip ( "x") a.lstrip a.rstrip ()
= A "the Hello World"
B = a.strip ()
Print (B)
>>> the Hello World

. 3, the corresponding character conversion: str.maketrans / Translate -----> STR

V = "abcdefghijklmnodsjjkkkee" original string #

m = str.maketrans ( "aeiou", ' 12345') # m configured corresponding relationship
new_v = v.translate (m) # generate a new relationship 
Print (new_v)
>>> 1bcd2fgh3jklmn4dsjjkkk22

. 4, Partition string divided in three stages ----> ancestral
v = "abcdefghijklmnodsjjkkkee"
b = v.partition("s")
print(b)
>>>('abcdefghijklmnod', 's', 'jjkkkee')
5, split split string, clean break, three sections twice, get segment element ----> list
= V "abcbdefbghijk" 
B = v.split ()
C = v.split ( 'B')
D = v.split ( 'B', 2)
Print (. 1, B)
Print (2, C)
Print (. 3, D)
>>>. 1 [ 'abcbdefbghijk']
>>> 2 [ 'A', 'C', 'DEF', 'ghijk']
>>>. 3 [ 'A', 'C', 'defbghijk']

. 6 , splitlines according to \ n divided -----> list
= A "abcsjdfk \ nfjsdjf \ njdfkdj \ nsdfsf" 
B = a.splitlines () (parameter may be True / False whether to keep \ n-)
Print (B)
for B in I: # text processing
  Print (I)
>>> [ 'abcsjdfk', 'fjsdjf', 'jdfkdj', 'sdfsf']

. 7, Find method: return int, the first index number
a = '123bcds'
b = a.find('b')
print(b,type(b))
>>>3 <class 'int'>
 
 

Guess you like

Origin www.cnblogs.com/qyan-blog/p/11649185.html