十二、python字符串汇总
'''
1. index():检测字符串str1中是否包含字符串str2
语法:str1.index(str2,beg,end)
str:指定检索的字符串;beg开始的索引,默认为0;end结束的索引,默认字符串的长度
如果字符串中包含,返回开始的索引值,否则跑抛出异常
'''
str1="this is a book"
str2="bo"
print str1.index(str2)
print str1.index(str2,8)
print str1.index(str2,11)
----------------------------------------
15
15
报错
--------------------------------------------------------------------------------------------------------------
'''
2.isalnum():检测字符串是否由字母和数字组成
string中至少有1个字符串,且所有字符都是字母或者数字或者字母数字组合,则返回True,否则返回false
'''
str="abc1905"
str1="abx"
str2="abc!@"
print str.isalnum()
print str1.isalnum()
print str2.isalnum()
----------------------------------------
True
True
False
--------------------------------------------------------------------------------------------------------------
'''
3.isalpha():检测字符串只有字母组成
string中至少有1个字符串,且所有字符都是字母,则返回True,否则返回false
'''
str="abc"
str1="abc123"
str2="abcABC"
print str.isalpha()
print str1.isalpha()
print str2.isalpha()
----------------------------------------
True
False
True
--------------------------------------------------------------------------------------------------------------
'''
4.isdigit():检测字符串只由数字组成
string中至少有1个字符串,且所有字符都是数字,则返回True,否则返回false
'''
str="123456789"
str1="abc123"
print str.isdigit()
print str1.isdigit()
----------------------------------------
True
False
--------------------------------------------------------------------------------------------------------------
'''
5.islower():检测字符串只由小写字母组成
string中至少有1个字符串,且所有字符都是小写字母,则返回True,否则返回false
'''
str="daffdsfds"
str1="afdfsdfSDADSA"
print str.islower()
print str1.islower()
----------------------------------------
True
False
--------------------------------------------------------------------------------------------------------------
'''
6.isnumeric():检测字符串只由数字组成,该方法只针对unicode对象
string中至少有1个字符串的unicode对象(仅需在对象前加“u”),且所有字符都是数字,则返回True,否则返回false
'''
str=u"abc1234"
str1=u"1223445"
str2="1332656"
print str.isnumeric()
print str1.isnumeric()
print str2.isnumeric()
----------------------------------------
False
True
Traceback (most recent call last):
File “D:/zhiliangbaozhangbu/06workspace/chushujin/test/��ϰ��/teststr.py”, line 67, in <module>
print str2.isnumeric()
AttributeError: ‘str’ object has no attribute ‘isnumeric’
--------------------------------------------------------------------------------------------------------------
'''
7.isspace():检测字符串只由空格组成
string中至少有1个字符串,且所有字符都是空格,则返回True,否则返回false
'''
str=" "
str1="123 abc"
print str.isspace()
print str1.isspace()
----------------------------------------
True
False
--------------------------------------------------------------------------------------------------------------
'''
8.istitle()检测字符串中所有字母的首字母都是大写,其余为小写
string中至少有1个字符串,且所有字符的首字母都是大写,其余为小写,则返回True,否则返回false
'''
str="This Is A Pig"
str1="This is a pig"
str2="THIS Pig"
str3="This Is 1234"
print str.istitle()
print str1.istitle()
print str2.istitle()
print str3.istitle()
----------------------------------------
True
False
False
True
--------------------------------------------------------------------------------------------------------------
'''
9.isupper():检测所有字符串的首字母都是大写
string中至少有1个字符串,且所有字符的字母都是大写,则返回True,否则返回false
'''
str="THIS IS A PIG"
str1="THis is a pig"
str2="THIS IS A 123456"
print str.isupper()
print str1.isupper()
print str2.isupper()
----------------------------------------
True
False
True
--------------------------------------------------------------------------------------------------------------
'''
10.join():用于将序列中的元素以指定的字符串生成一个新的字符串
语法:str.join(seq)--seq为要连接的字符列表,str作为整体,穿插在seq每个字符串中间
str.join(seq)之后生成的只能是字符串,字典只能对键进行连接
'''
str="-"
seq="abcd"
wow="123"
print str.join(seq)
print seq.join(wow)
list=['1','2','3','4','5']
print(''.join(list))
seq = {'hello':'nihao','good':2,'boy':3,'doiido':4}
print('-'.join(seq)) #字典只对键进行连接
----------------------------------------
a-b-c-d
1abcd2abcd3
12345
boy-good-doiido-hello
--------------------------------------------------------------------------------------------------------------