python数据类型常用内置函数之字符串

1、strip, lstrip, rstrip

x = '       jiahuifeng    '
print(x.strip(' '))
print(x.lstrip(' '))
print(x.rstrip(' '))
View Code

2、lower ,upper

x = 'jiahuifeng'
print(x.lower())
print(x.upper())
View Code

3、startswith,endswith

x = 'fengjiahui_haha'
print(x.startswith('fe'))
print(x.endswith('_haha'))
View Code

4、格式化输出

print('My name is %s,my age is %s' %('jiahuifeng',18))
print('{} {} {}'.format('jiahuifeng',18,'male'))
View Code

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())
View Code

6、join

name=' '
print(tag.join(['egon','say','hello','world']))


-->egon say hello world
View Code

7、replace

name='jiahuifeng say :i have one apple,my name is jiahuifeng'
print(name.replace('jiahuifeng','qiqi',1))
View Code

8、拼接

x = 'jiahuifeng'
y = 'shi'
z = 'haha'

name = x+y+z
print(name)
View Code

 

posted @ 2019-01-07 16:35  jiahuifeng  阅读(158)  评论(0编辑  收藏  举报