Python method to remove the string on spaces

Python method to remove the string on spaces

When programming, we often encounter situations need to remove the spaces in the string, we can usually use the following several solutions:

 

. 1 , Strip () method: This method can only spaces string head and tail removed, but can not remove the intermediate space string.

s=' This is a demo '

print(s.strip())

 

 

 

 

 

lstrip () : This method can only string left-most spaces removed.

s=' ! This is a demo '

l='!'

print(s.lstrip()+l)

 

 

 

 

rstrip () : This method can only spaces rightmost string removed.

s=' ! This is a demo '

l='!'

print(s.rstrip()+l)

 

 

 

 

2.replace (m, n) Method: inside the string m is replaced with n- .

# Will remove all spaces in the string 

S = ' This Demo IS A ' 

Print (s.replace does ( '  ' , '' ))

 

 

 

 

 

 3.split (S , NUM) Method: Split ( S, NUM )

# Using join () method to remove all spaces in the string 

S = ' This Demo IS A ' 

Print ( '' .join (s.split ()))

 

 

 

 

Wherein, join () method is used to specify the elements of a sequence is connected to generate a new character string.

Format is as follows:

s.join(sequence)

Is a separator between the elements S , Sequence is the sequence of elements to be connected.

# "=" Is connecting string sequence 

S = " = " 

# String sequence 

SEQ = ( " A " , " B " , " C " ) 

Print (s.join (SEQ))

 

 

 

Guess you like

Origin www.cnblogs.com/BIXIABUMO/p/11789805.html