
正文
python全栈开发从入门到放弃之字符串的应用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1、strip
strip 脱去(...的)衣服
去掉左右两边的空白msg=' hellprint(msg)
1 print(msg.strip()) #去掉左右两边的空白
2
3 hello
4 hello
5
6 msg='********hello************'
7 print(msg)
8 print(msg.strip('*')) #指定的去掉左右两边的*
9 ********hello************
10
11 hello
12 (1)lstrip 指定去掉左边的*
13 # print(msg.lstrip('*'))
14 msg='********hello************'
15 print(msg)
16 print(msg.lstrip('*')) #指定去掉左边的*
17 ********hello************
18 hello************
19
20 (2)rstrip 指定去掉右边的连续的*
21 # print(msg.rstrip('*'))
22 msg='********hello************'
23 print(msg)
24 print(msg.rstrip('*')) #指定去掉右边的连续的*
25 ********hello************
26 ********hello
#strip的用处
# while True:
# name=input('user: ').strip()
# password=input('password: ').strip()
#
# if name == 'egon' and password == '123':
# print('login successfull')
2、split 切分
1 info='root:x:0:0::/root:/bin/bash'
2 print(info[0]+info[1]+info[2]+info[3])
3 root info='root:x:0:0::/root:/bin/bash'
print(info[0]+info[1]+info[2]+info[3]) info='root:x:0:0::/root:/bin/bash'
print(info[0]+info[1]+info[2]+info[3])
info='root:x:0:0::/root:/bin/bash'
print(info[0]+info[1]+info[2]+info[3]) user_l=info.split(':') #用内置方法以‘:’能把字符串类型以列表的形式呈现出来,来取值
print(user_l[0]) # msg='hello world egon say hahah'
# print(msg.split()) #默认以空格作为分隔符
msg='hello world egon say hahah'
print(msg.split()) #默认以空格为分隔符
['hello', 'world', 'egon', 'say', 'hahah'] cmd='download|xhp.mov|3000'
# cmd_l=cmd.split('|')
# print(cmd_l[1])
# print(cmd_l[0]) # print(cmd.split('|',1))
用处
# while True:
# cmd=input('>>: ').strip()
# if len(cmd) == 0:continue
# cmd_l=cmd.split()
# print('命令是:%s 命令的参数是:%s' %(cmd_l[0],cmd_l[1]))
3、len 长度
1 # print(len('hell 123'))
2 print(len('hell 123')) #查看字符串的长度
8 #字符串长度为8
4、索引
根据索引来取出子字符串
msg='hello world
print(msg[1:3]) #1 2
print(msg[1:4]) #1 2 3
msg='hello world'
print(msg[1:3]) #切出字符串子集但顾头不顾尾
el
5、混合应用
oldboy_age=84
while True:
age=input('>>: ').strip()
if len(age) == 0:continue
if age.isdigit():
age=int(age)
else:
print('must be int')
6、endswith
#startswith,endswith
# name='alex_SB'
# print(name.endswith('SB')) #查找字符串结尾里是否有输入的参数,有着返回True,没有返回False
# print(name.startswith('alex')) #查找开头是否有输入的参数,有着返回True,没有返回False
7、replace
#replace
# name='alex say :i have one tesla,my name is alex'
# print(name.replace('alex','SB',1))
name = 'alex say :i have one tesla,my name is alex'
print(name.replace('alex','SB',1)) #在开头和结尾添加指定字符
SB say :i have one tesla,my name is alex
8、占位符
# %s对应所有字符 一一对应后面的元素
print('my name is %s my age is %s my sex is %s' %('egon',18,'male'))
#把{}当作一个占位符,.format调用对应每一个后面输入的字符
print('my name is {} my age is {} my sex is {}'.format('egon',18,'male'))
my name is egon my age is 18 my sex is male
#根据{}里的索引来对应值,当{}没有值时默认为一一对应。
print('my name is {0} my age is {1} my sex is {0}:{2}'.format('egon',18,'male'))
my name is egon my age is 18 my sex is egon:male
#根据{}里的输入的key来准确调用key对应的元素
print('my name is {name} my age is {age} my sex is {sex}'.format(
sex='male',
age=18,
name='egon'))
my name is egon my age is 18 my sex is male
9、find、index、count查找
#可以指定范围查找但顾头不顾尾,找到则返回该值的索引号,没有则返回-1不报错,
# find 查找
name='goee say hello'
print(name.find('e',1,3))
#index 查找
可以指定范围查找但顾头不顾尾,找到则返回该字符串的索引号,没有则报错
name='goee say hello'
print(name.index('s')) #count
指定参数,查找字符串里的元素,有则返回这个查找的值里有多少数,有1个就返回1,2个就返回2 等等,没有则返回0
name='aaaaaaaa aaaaa aaa'
print(name.count('a'))
16
10、join
#join
# info='root:x:0:0::/root:/bin/bash'
# print(info.split(':')) # l=['root', 'x', '0', '0', '', '/root', '/bin/bash']
# print(':'.join(l)) #join 两个物体连接处
指定分隔符默认,用:连接起来
l=['root', 'x', '', '', '', '/root', '/bin/bash']
print(':'.join(l))
root:x:0:0::/root:/bin/bash
11、lower,upper大小写转换
#lower,upper
# name='eGon'
# print(name.lower())
# print(name.upper())
将大写字母全转为小写字母
name = 'Egon'
print(name.lower())
egon 将小写字母全转为大写
name = 'Egon'
print(name.upper())
了解部分
1、 expandtabs 指定参数多少个空格符
name='egon\thello'
print(name)
print(name.expandtabs(3))
egon hello
egon hello
2、center,ljust,rjust,zfill
以原有的变量的字符为中心两边添加指定数量的符号
name='egon'
print(name.center(30,'_'))
_____________egon_____________ ljust
原有元素左对齐后面添加指定数量的符号
name='egon'
print(name.ljust(30,'*'))
egon**************************
rjust
原有元素右对齐,往左边添加指定数量的符号
name='egon'
print(name.rjust(30,'*'))
3、captalize,swapcase,title
captalize
首字母大写,其余部分小写
name='eGOn'
print(name.capitalize())
Egon title
每个单词的首字母大写
msg='egon say hi'
print(msg.title()) swapcase
小写互相对调大写变小写,小写变大写
name='eGOn'
print(name.swapcase())
EgoN
#在python3中
4、元素判断
1 isdigit 是否为数字
2 判断字符串里是否为数字,是则返回True,不是则返回False
3
4 num0=''
5 num1=b'' #bytes
6 num2=u'' #unicode,python3中无需加u就是unicode
7 num3='四' #中文数字
8 num4='Ⅳ' #罗马数字
9
10 print(num0.isdigit())
11 print(num1.isdigit())
12 print(num2.isdigit())
13 print(num3.isdigit())
14 print(num4.isdigit())
#isdecimal:str,unicode
# num0='4'
# num1=b'4' #bytes
# num2=u'4' #unicode,python3中无需加u就是unicode
# num3='四' #中文数字
# num4='Ⅳ' #罗马数字
# print(num0.isdecimal())
# # print(num1.)
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal()) isdecimal
判断一个数是否为十进制数,是则返回True,不是则返回False
num0=''
num1=b''
num2=u''
num3='四'
num4='Ⅳ'
print(num0.isdecimal())
print(type(num1))
print(num2.isdecimal())
print(num3.isdecimal())
print(num4.isdecimal())
True
<class 'bytes'>
True
False
False #isnumeric:str,unicode,中文,罗马
# num0='4'
# num1=b'4' #bytes
# num2=u'4' #unicode,python3中无需加u就是unicode
# num3='四' #中文数字
# num4='Ⅳ' #罗马数字
#
# print(num0.isnumeric())
# # print(num1)
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric()) isnumeric 同质异构的
只针对与unicode字符的判断是否为数字,是则为真不是则为假
num0=''
num1=b''
num2=u''
num3='四'
num4='Ⅳ'
print(num0.isnumeric())
print(type(num1))
print(num2.isnumeric())
print(num3.isnumeric())
print(num4.isnumeric())
True
<class 'bytes'>
True
True
True
5、is其他 isalnum、isalpha
# name='egon123'
# print(name.isalnum()) #字符串由字母和数字组成
# name='asdfasdfa sdf'
# print(name.isalpha()) #字符串只由字母组成
#
isalnum 字符测试
测试变量里的元素是否为字母和数字组成
name = 'egon123'
print(name.isalnum())
True






