Contents
20.2.5. 删除字符串中不需要的字符¶
#!/usr/bin/env python
# -*- coding:utf8 -*-
# auther; 18793
# Date:2019/7/30 9:41
# filename: 05.删除字符串中不需要的字符.py
s = ' hello world \n'
print(s.strip())
print(s.lstrip())
print(s.rstrip())
t = '-----hello====='
print(t.strip('-'))
print(t.strip('='))
print(t.strip('-='))
s = ' hello world \n'
print(s.strip())
print(s.replace(' ', '').strip())
import re
print(re.sub('\s+', ' ', s))
输出信息
hello world
hello world
hello world
hello=====
-----hello
hello
hello world
helloworld
hello world
比如从文件中读取多行数据。 如果是这样的话,那么生成器表达式就可以大显身手了