python版本 3.5
#Author by Liguangbo#_*_ coding:utf-8 _*_str="i like study python, welcome to my python program\t."#首字母大写print(str.capitalize())#I like study python, welcome to my python program.#关键字在字符串中出现的次数print(str.count(" "))#8#打印100个字符,如果str不够,则用-代替,且字符str位于中间print(‘hello world‘.center(20,‘-‘))#----hello world-----#判断字符串是否以‘l’和‘.’开头结尾print(str.startswith(‘l‘))#Falseprint(str.endswith(‘.‘))#True#将tab键转为5个空格print(str.expandtabs(tabsize=51))#i like study python, welcome to my python program .#查找第一个sub出现的位置sub=‘p‘print(str[str.find(sub):])#python, welcome to my python program .#字符串的参数调用及赋值s="my name is {name},i am {years} years old!"print(s.format(name="ligb",years="28"))print(s.format_map({‘name‘: ‘ligb‘ ,‘years‘:28}))#my name is ligb,i am 28 years old!#判断是否是由阿拉伯数字或字母组成,不能包含符号、空格x=‘我‘print(x.isalnum())#True#判断是否是纯字符,不能包含数字或者符号print(x.isalpha())#Trueprint(‘一‘.isdecimal())#Falseprint(‘1‘.isdigit())#True#判断是否是小写、大写print(‘a‘.islower())#Trueprint(‘a‘.isupper())#False#判断是否所有单词首字母大写print(‘My Name Is ‘.istitle())#True#判断文件是否可以打印print(‘my name is ligb‘.isprintable())#tty drive等文件不可打印#True#列表转字符串print(‘%‘.join([‘wo‘,‘men‘,‘de‘,‘jia‘]))#wo%men%de%jia#若字符串长度不够20,则在末尾加*补充print(‘hello world‘.ljust(20,‘*‘))#hello world*********print(‘hello world‘.rjust(20,‘*‘))#*********hello world#大小写转换print(‘hello world‘.lower())print(‘hello world‘.upper())#hello world#HELLO WORLD#去掉首尾的回车或者换行print(‘ hello world\n‘.strip())print(‘-----‘)#hello world#-----#去掉左右的回车或者换行print(‘ hello world\n‘.rstrip())print(‘ hello world\n‘.lstrip()) #查找最右边的关键字print(‘hello world !‘.rfind(‘world‘))#以空格为分割符,生成列表print(‘ ‘.join(‘hello world my name is‘.split()))print(‘hello world my name is‘.split())#[‘hello‘, ‘world‘, ‘my‘, ‘name‘, ‘is‘]print(‘hello+world+my+name+is‘.split(‘+‘))#[‘hello‘, ‘world‘, ‘my‘, ‘name‘, ‘is‘]#按照换行来分print(‘hello \n world‘.splitlines())#[‘hello ‘, ‘ world‘]#调换大小写print(‘Hello World‘.swapcase())#hELLO wORLDprint(‘hello world‘.title())#Hello World
时间: 2024-10-23 02:48:19