判断字符
函数 | 含义 |
字符串.isalnum() | 所有字符都是数字或者字母,为真返回 Ture,否则返回 False |
字符串.isalpha() | 所有字符都是字母,为真返回 Ture,否则返回 False |
字符串.isdigit() | 所有字符都是数字,为真返回 Ture,否则返回 False |
字符串.islower() | 所有字符都是小写,为真返回 Ture,否则返回 False |
字符串.isupper() | 所有字符都是大写,为真返回 Ture,否则返回 False |
字符串.istitle() | 所有的单词拼写首字母是否为大写,且其他字母为小写,为真返回 Ture,否则返回 False |
字符串.isspace() | 所有字符都是空白字符,为真返回 Ture,否则返回 False |
字符串链接的8中方式
1. str1+str2
string类型 ‘+’号连接
>>> str1="one"
>>> str2="two"
>>> str1+str2
'onetwo'
2. str1,str2
string类型 ‘,’号连接成tuple类型
>>> str1="one"
>>> str2="two"
>>> str1,str2
('one','two')
>>> type((str1,str2))
<type 'tuple'>
3. 格式化字符串连接
string类型格式化连接
1.常见的格式化方式
>>> str1="one"
>>> str2="two"
>>> "%s%s"%(str1,str2)
'onetwo'
2.高级点的format 格式化
>>> "{test}_666@{data:.2f}".format(test="Land", data=10.1)
'Land_666@10.10'
3.鲜为人知的【%(word)type】print函数格式化
>>> print "%(test)s666%(last)d" % {"test": "Land", "last": 101}
Land666101
4. str1 str2
string类型空格自动连接
>>> "one" "two"
'onetwo'
5. str1 \ str2 \str3
string类型反斜线多行连接
>>> test = "str1 " \
... "str2 " \
... "str3"
>>> test
'str1 str2 str3'
6. M*str1*N
string类型乘法连接
>>> str1="one"
>>> 1*str1*4
'oneoneoneone'
7. join方式连接
string类型join方式连接list/tuple类型,连接的列表或元组中元素的类型必须全部为string类型
>>> str1="one"
>>> list1=["a","b","c"]
>>> tuple1=("H","I","J")
>>> str1.join(list1)
'aonebonec'
>>> str1.join(tuple1)
'HoneIoneJ'
>>> list1
['a', 'b', 'c']
>>> "".join(list1)
'abc'
8.列表推导方式连接
与join方式类似
>>> "".join(["Land" for i in xrange(3)])
'LandLandLand'
>>> "0".join(["Land" for i in xrange(2)])
'Land0Land'