python关于字符串的操作

it2024-10-15  20

#-*- coding:utf-8 -*-#Author:gxli#字符串的操作name=' zhangsan,lisi,wangwu '#分割操作name=name.split(',')print(name)#[' zhangsan', 'lisi', 'wangwu ']#列表转字符串拼接name='|'.join(name)print(name)# zhangsan|lisi|wangwu#去除开头结尾制定字符name2=' lgx 'name2=name2.strip()#name2=name2.strip(' ')print(len(name2))#3#特殊显示print(name2.center(40,'-'))#------------------lgx-------------------#查找字符并显示字符的下标print(name2.find('g'))#1#判断是否包含空格print(' ' in name2)#False#首字母大写print(name2.capitalize())#Lgx#格式化显示msg="hello, {name} it's been along {age}"msg2=msg.format(name='lll',age=22)print(msg2)#isdigit()判断是否为数字age=input('your age:')if age.isdigit(): age=int(age)else: print('invalid data type')#判断是否包含违法字符name3='ale3sdf'print(name3.isalnum())#判断是否已dfsd结尾print(name3.endswith('dfsd'))#判断是否已dfsd开头print(name3.startswith('dfsd'))#大小写转换print(name3.upper().lower())

转载于:https://www.cnblogs.com/liguangxu/p/5495758.html

最新回复(0)