python数据类型常用内置函数之字符串
1、strip, lstrip, rstrip
x = ' jiahuifeng ' print(x.strip(' ')) print(x.lstrip(' ')) print(x.rstrip(' '))
2、lower ,upper
x = 'jiahuifeng' print(x.lower()) print(x.upper())
3、startswith,endswith
x = 'fengjiahui_haha' print(x.startswith('fe')) print(x.endswith('_haha'))
4、格式化输出
print('My name is %s,my age is %s' %('jiahuifeng',18)) print('{} {} {}'.format('jiahuifeng',18,'male'))
5、split , splitlines
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
name=' ' print(tag.join(['egon','say','hello','world'])) -->egon say hello world
7、replace
name='jiahuifeng say :i have one apple,my name is jiahuifeng' print(name.replace('jiahuifeng','qiqi',1))
8、拼接
x = 'jiahuifeng' y = 'shi' z = 'haha' name = x+y+z print(name)