字符串01
//全部变小写
>>> x='i love U'
>>> x.casefold()
'i love u'
//首字母大写
>>> x='i love U'
>>> x.title()
'I Love U'
//大写变小写,小写变大写
>>> x='i love U'
>>> x.swapcase()
'I LOVE u'
//全部变小写
>>> x='i love U'
>>> x.lower()
'i love u'
//三个字节居中
>>> x='i love U'
>>> x.center(3)
'i love U'
//20字节居中
>>> x='i love U'
>>> x.center(20)
' i love U
//20字节左对齐
>>> x='i love U'
>>> x.ljust(20)
'i love U
//20字节右对齐
>>> x='i love U'
>>> x.rjust(20)
' i love U'
//20字节右对齐,000补空
>>> x='i love U'
>>> x.zfill(20)
'000000000000i love U'
//20字节居中,哈哈哈补空
>>> x='i love U'
>>> x.center(20,'哈')
'哈哈哈哈哈哈i love U哈哈哈哈哈哈'
//20字节左对齐,哈哈哈补空
>>> x='i love U'
>>> x.ljust(20,'哈')
'i love U哈哈哈哈哈哈哈哈哈哈哈哈'
查找
//查找字符串中海的个数
>>> x='上海自来水来自海上'
>>> x.count('海')
2
//查找第0~5个字中海的个数
>>> x='上海自来水来自海上'
>>> x.count("海",0,5)
1
//正向查找海的个数
>>> x='上海自来水来自海上'
>>> x.find("海")
1
//反向查找海的个数
>>> x='上海自来水来自海上'
>>> x.rfind("海")
7
//查找不存在的字
>>> x='上海自来水来自海上'
>>> x.find("哈")
-1
//查找不存在的字
>>> x='上海自来水来自海上'
>>> x.index("哈")
Traceback (most recent call last):
File "<pyshell#94>", line 1, in <module>
x.index("哈")
ValueError: substring not found
替换
//替换指令
>>> "在吗,我在你家楼下。".replace("在吗","想你")
'想你,我在你家楼下。'
//将大写的字母由A替换成B
>>> table=str.maketrans("ABCDEFG","1234567")
>>>table=str.maketrans("ABCDEFGHIL","1234567890")
>>>> "I love Hahaha".translate(table)
'9 love 8ahaha'
"I Love Hahaha".translate(str.maketrans("ABCDEFGHIL","1234567890"))
'9 0ove 8ahaha'
>>> "I Love Hahaha".translate(str.maketrans("ABCDEFGHIL","1234567890","Love"))
'9 8ahaha'
>>> "在吗,我在你家楼下。".replace("在吗","想你")
'想你,我在你家楼下。'
>>> table=str.maketrans("ABCDEFG","1234567")
>>> "I love Hahaha".translate(table)
'I love Hahaha'
>>> table=str.maketrans("ABCDEFGHIL","1234567890")
>>> "I love Hahaha".translate(table)
'9 love 8ahaha'
>>> "I Love Hahaha".translate(str.maketrans("ABCDEFGHIL","1234567890"))
'9 0ove 8ahaha'
>>> "I Love Hahaha".translate(str.maketrans("ABCDEFGHIL","1234567890","Love"))
'9 8ahaha'
判断
startswith(prefix[,start[,end]])
endswith(suffix[,start[,end]])
>>> x='I LOVE Python'
>>> x.startswith('I')
True
判断开头字母是什么
>>> x.endswith("Python")
True
>>> x.endswith("python")
False
判断结尾是不是Python
>>> x.startswith('I',2)
False
>>> x.startswith('LOVE',2)
True
判断从头开始第二个是不是“LOVE”
>>> x.endswith("Py",0,9)
True
>>> x="我爱python"
>if x.startswith(("你","我","他")):
print('总有人喜欢python')
总有人喜欢python
判断X中是不是有关键字,有->输入。
>>> y="I love Python"
>>> y.istitle()
False
False的原因是love的l不是大写
>>> y="I love Python"
>>>> y.isupper()
False
>>> y.upper()
'I LOVE PYTHON'
>>> y.upper().isupper()
True
upper()将字符串转换为大写
>>> y="I love Python"
>>>> y.isalpha() ""判断是否是全英文""
False
>>> y.isspace()
False
>>> x=" " ""空格、TAB内容""
>>> x.isspace() ""判断空格、TAB内容""
True
>>> "判断是否可以打印"
"I LOVE YOU\n".isprintable()
>>> x="12345"
>>> x.isdecimal()
True
>>> x.isdigit()
True
>>> x.isnumeric()
True
>>> x="2²"
>>> x.isdecimal()
False
>>> x.isdigit()
True
>>> x.isnumeric()
True
>>> x="ⅠⅡⅢⅣⅤ"
>>> x.isdecimal()
False
>>> x.isdigit()
False
>>> x.isnumeric()
True
>>> x="一二三四五"
>>> x.isdecimal()
False
>>> x.isdigit()
False
>>> x.isnumeric()
True
>>> x.isalnum()
True
>>> "只要isdecimal()、isdigit()、isnumeric()、isalpha()其中一个可以isnumeric()就输出Trun"
>>> "I AM A GOOD BOY".isidentifier()
False
>>> "I_AM_A_GOOD_BOY".isidentifier()
True
>>> "520I_AM_A_GOOD_BOY".isidentifier()
False
>>> "开头不能是数字"
'判断是不是Pyhon的保留标识符'
>>> import keyword
>>> keyword.iskeyword("if")
True
>>> keyword.iskeyword("py")
False