python数据类型常用内置函数之字符串
1、strip, lstrip, rstrip
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
x = ' jiahuifeng ' print(x.strip(' ')) print(x.lstrip(' ')) print(x.rstrip(' '))
2、lower ,upper
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
x = 'jiahuifeng' print(x.lower()) print(x.upper())
3、startswith,endswith
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
x = 'fengjiahui_haha' print(x.startswith('fe')) print(x.endswith('_haha'))
4、格式化输出
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
print('My name is %s,my age is %s' %('jiahuifeng',18)) print('{} {} {}'.format('jiahuifeng',18,'male'))
5、split , splitlines
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name='root:x:0:0::/root:/bin/bash' print(name.split(':')) #默认分隔符为空格 name='C:/a/b/c/d.txt' #只想拿到顶级目录 print(name.split('/',1)) name='a|b|c' print(name.rsplit('|',1)) #从右开始切分 name = '''ab c de fgkl sadfewf''' print (name.splitlines())
6、join
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name=' ' print(tag.join(['egon','say','hello','world'])) -->egon say hello world
7、replace
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name='jiahuifeng say :i have one apple,my name is jiahuifeng' print(name.replace('jiahuifeng','qiqi',1))
8、拼接
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
x = 'jiahuifeng' y = 'shi' z = 'haha' name = x+y+z print(name)