一、用于判断的方法,返回值为bool类型
isalnum()
'''是否是字母或数字'''
>>> str = "abc"
>>> str.isalnum()
True
>>> str = "123"
>>> str.isalnum()
True
>>> str = "___"
>>> str.isalnum()
False
isalpha()
'''是否是字母'''
>>> str = "abc"
>>> str.isalpha()
True
>>> str = "123"
>>> str.isalpha()
False
isdigit()
'''是否是数字'''
>>> str = "abc"
>>> str.isdigit()
False
>>> str = "123"
>>> str.isdigit()
True
islower()
'''是否全部是小写'''
>>> str = "abc"
>>> str.islower()
True
>>> str = "aBc"
>>> str.islower()
False
isupper()
'''是否全部是大写'''
>>> str = "ABC"
>>> str.isupper()
True
>>> str = "AbC"
>>> str.isupper()
False
isspace()
'''是否是空格'''
>>> str = " "
>>> str.isspace()
True
>>> str = "a b"
>>> str.isspace()
False
istitle()
'''是否每个单词的首字母都是大写'''
>>> str = "This Is A Title"
>>> str.istitle()
True
>>> str = "This is A Title"
>>> str.istitle()
__contains__()
'''判断参数是否在原字符串中'''
>>> str_1 = "this is a statement"
>>> str_2 = "is"
>>> str_3 = "was"
>>> str_1.__contains__(str_2)
True
>>> str_1.__contains__(str_3)
False
__eq__()
'''判断两字符串是否相等'''
>>> str_1 = "abc"
>>> str_2 = "abc"
>>> str_3 = "def"
>>> str_1.__eq__(str_2)
True
>>> str_1.__eq__(str_3)
False
endswith()
'''判断字符串是否以某一子序列结尾,可使用第二个参数规定起始位置(包含),第三个参数规定结束位置(不包含)'''
>>> str = "abcdef"
>>> str.endswith("ef")
True
>>> str.endswith("ef",0,4)
False
startswith()
'''判断字符串是否以某一子序列开头,可使用第二个参数规定起始位置(包含),第三个参数规定结束位置(不包含)'''
>>> str = "abcdef"
>>> str.startswith("bc")
False
>>> str.startswith("bc",1,7)
True
二、其它常用方法
capitalize()
'''将字符串的首字母变为大写'''
>>> str = "this is a statement"
>>> str.capitalize()
'This is a statement'
casefold()
'''将字符串的首字母变为小写'''
>>> str = "This is a statement"
>>> str.casefold()
'this is a statement'
center()
'''在第一个参数规定的长度居中,用第二个参数规定的字符串填充空白'''
>>> str = "this is a statement"
>>> str = "abc"
>>> str.center(20,"*")
'********abc*********'
count()
'''计算字符串中某个子序列出现的次数,可使用第二个参数指定起始位置(包含),第三个参数指定结束位置(不包含)'''
>>> str = "abcdabcdabcd"
>>> str.count("ab",2,5)
0
>>> str.count("ab",2,10)
2
encode()
'''编码转换,参数为目的编码'''
>>> str = "abc"
>>> str.encode("gbk")
expandtabs()
'''将字符串中的Tab符(\t)转换为参数指定的空格数,默认为8个'''
>>> str = "\tabc"
>>> str.expandtabs(4)
' abc'
find()
'''在字符串中查找某个子序列的位置并返回索引值,若找不到则返回-1'''
>>> str = "abcdef"
>>> str.find("cd")
2
>>> str.find("g")
-1
index()
'''在字符串中查找某个子序列的位置并返回索引值,若找不到则抛出异常'''
>>> str = "abcdef"
>>> str.index("cd")
2
>>> str.index("g")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
format()
'''字符串格式化,逐个拼接{n},第一个为{0}'''
>>> str = "This {0} a {1}"
>>> str.format("is","statement")
'This is a statement'
join()
'''将字符串对象拼接到参数指定的列表中,列表的每个元素以该字符串分隔,返回拼接完成后的字符串'''
>>> list = ["this","is","a","statement"]
>>> "__".join(list)
'this__is__a__statement'
lower()
'''将字符串变为小写'''
>>> str = "ABC"
>>> str.lower()
'abc'
upper()
'''将字符串变为大写'''
>>> str = "abc"
>>> str.upper()
'ABC'
partition()
'''将字符串以某个子序列进行分割,参数为要分割字符串的子序列,返回一个元组'''
>>> str = "abcdef"
>>> str.partition("cd")
('ab', 'cd', 'ef')
replace()
'''将字符串中第一个参数指定的子序列全部替换为第二个参数指定的子序列,可用第三个参数规定转换的组序列个数'''
>>> str = "abcabcabcabc"
>>> str.replace("ab","A",2)
'AcAcabcabc'
split()
'''指定使用某个子序列分割字符串并删除该子序列,返回一个列表'''
>>> str = "abcdef"
>>> str.split("cd")
['ab', 'ef']
splitlines()
'''将多行字符串按行进行分割,返回一个列表'''
>>> str = '''These
... are
... four
... lines'''
>>> str.splitlines()
['These', 'are', 'four', 'lines']
swapcase()
'''将所有字符大小写反转'''
>>> str = "abCDef"
>>> str.swapcase()
'ABcdEF'
title()
'''将字符串中所有单词的首字母变为大写'''
>>> str = "this is a title"
>>> str.title()
'This Is A Title'