s = ' sdf sdf ' print(s.strip()) #去掉字符串首尾的空白 s1 = 'hello,maco' print(s1.split(',')) #拆分字符串:以逗号对s1进行分割(默认以空格进行分割)。分割之后形成列表 li = ['hello','maco','haha'] print('|'.join(li)) #合并字符串:以'|'将列表中的元素合并(默认直接拼接:''.join()) s2 = 'hello world' print(s2.capitalize()) #字符串首字母设置大写(首字母已大写的就不变) msg = 'hello {name},your {car} is so good!' print(msg.format(name='maco',car='bike')) #格式化字符串 #结果:hello maco,your bike is so good! msg2 = '{0} is a good man,he is {1}' print(msg2.format('Maco','26')) #另一种形式的格式化字符串 #结果:Maco is a good man,he is 26 s3 = 'my name is Maco Lee!' print(s3[3:7]) #字符串切片 s4 = 'Menu' print(s4.center(40,'-')) #整个字符串长度40,如果不满40,s4居中,其他位置就用'-'均匀的填充 s5 = 'Maco Lee' print(s5.find('L')) #查找s5种的'L',存在返回其下标,不存在返回-1 age = input('input your age:') if age.isdigit(): #判断字符串是不是数字形式的 age = int(age) else: print('invalid data type') name = 'maco3sDDSf' print(name.isalnum()) #如果字符串中没有特殊字符,返回True print(name.endswith('df')) #如果name是以‘df’结尾,返回True print(name.startswith('ma')) #如果name是以‘ma’开头,返回True print(name.upper()) #将字符串中的所有字符都转为大写 print(name.lower()) #将字符串中的所有字符都转为小写