字符串需要掌握的内置方法
作者:互联网
1、lstrip和rstrip
2、lower和upper
3、startswith和endswith
4、rsplit
5、join
6、replace
7、isdigit
"""
需要掌握的方法如下:
1、lstrip和rstrip
2、lower和upper
3、startswith和endswith
4、rsplit
5、join
6、replace
7、isdigit
"""
1、lstrip和rstrip
name='&&nick&&'
print(f"nick.lstrip('&'):{name.lstrip('&')}")
print(f"nick.rstrip('&'):{name.rstrip('&')}")
2、lower和upper
name='luzhaoshAN'
print(f"name.lower:{name.lower()}")
print(f"name.upper:{name.upper()}")
3、startswith和endswith
name='Nick Chen'
print(f"name.startswith(Nick):{name.startswith('N')}")
print(f"name.endswith(Chen):{name.endswith('m')}")
4、rsplit() 从右开始切割
info='nick:male:19'
print(f"info.rsplit(:,1):{info.rsplit(':',1)}")
5、join()
lis1=[1,2,'19']
print(f"':',join(lis):{':'.join(lis)}")#报错数字不可与字符串拼接
lis = ['nick', 'male', '19']
print(f"{':'.join(lis)}")
6、replace()
name='nick shuai'
print(f"name.replace('shuai','handsome'):{name.replace('shuai','handsome')}")
7、isdigit() 判断输入是否是数字结果为true或false
salary='1111'
print(salary.isdigit())
salary2='111.1'
print(salary.isdigit())
标签:upper,内置,join,name,nick,lstrip,掌握,print,字符串 来源: https://www.cnblogs.com/ludundun/p/11385204.html