每天CookBook之Python-031
- 使用strip、lstrip、rstrip替换空格
- 使用正则表达式替换换行
import re
s = '   hello world  \n'
print(s.strip())
print(s.lstrip())
print(s.rstrip())
t = '-----hello====='
print(t.lstrip('-'))
print(t.strip('-='))
s = '  hello       world   \n'
s = s.strip()
print(s)
print(s.replace(' ', ''))
print(re.sub('\s+', ' ', s))
hello world
hello world  
   hello world
hello=====
hello
hello       world
helloworld
hello world
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号