字符串内置函数
1.capitalize():把字符串的第一个字符大写
>>> str1='today is monday!'
>>> print 'str.capitalize():',str1.capitalize()
str.capitalize(): Today is monday!
>>>
2.center(width[,fillchar]):返回一个原字符串居中,并使用空格填充至长度width的新字符串
>>> str1='today is monday!'
>>> print 'str1.center():',str1.center(40,'a')
str1.center(): aaaaaaaaaaaatoday is monday!aaaaaaaaaaaa
>>>
3.count(str,beg=0,end=len(string)):返回str在string里面出现的次数,如果beg或者end指定范围,则返回指定
范围内str出现的次数。
>>> str1='today is monday!'
>>> print 'str1.count():',str1.count(sub,4,20)
str1.count(): 1
>>> sub='y'
>>> print 'str1.count():',str1.count(sub,4,40)
str1.count(): 2
>>>
4.decode(encoding='UTF-8',errors='strict'):以encoding指定的编码格式解码字符串。如果出错,默认报一个valueerror的异常。
5.encode(encoding='UTF-8'):以encoding指定的编码格式编码字符串。
str1='today is monday!'
>>> str1=str1.encode('base64','strict')
>>> print "Encoded string:"+str1
Encoded string:dG9kYXkgaXMgbW9uZGF5IQ==
>>> print "Decoded string:"+str1.decode('base64','strict')
Decoded string:today is monday!
>>>
6.endswith(obj,beg=0,end=len(string)):检查字符串是否以obj结束。如果是,返回True,否则返回false。
>>> str1='today is monday!'
>>> suffix='!'
>>> print str1.endswith(suffix)
True
>>> print str1.endswith(suffix,3,30)
True
>>> print str1.endswith(suffix,3,10)
False
>>>
7.expandtabs(tabsize=8):把字符串中tab符(\t)转为空格,默认的空格数tabsize是8.
8.find(str,beg=0,end=len(string)):检测str是否包含在string中,如果beg和end指定范围,则在指定范围内检测。如果是返回开始的索引值,否则返回-1.
>>> str1='today is monday!'
>>> suffix='!'
>>> print str1.find(suffix)
15
>>> print str1.find(suffix,40)
-1
>>>
9.index(str,beg=0,end=len(string)):和find方法一样,不过如果str不在string中会报一个异常。
>>> str1='today is monday!'
>>> suffix='!'
>>> print str1.index(suffix,40)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
print str1.index(suffix,40)
ValueError: substring not found
>>> print str1.index(suffix)
15
>>>
10.isalnum():string至少有一个字符并且所有字符都是字母或者数字则返回True,否则返回False。
>>> str1='thisis2015'
>>> print str1.isalnum()
True
>>> str2='thisis 2015'
>>> print str2.isalnum()
False
>>>
11.isalpha():如果string至于有一个字符并且所有字符都是字母则返回True,否则返回False。
>>> str1='thisis2015'
>>> print str1.isalpha()
False
>>> str3='thisisaexample'
>>> str3.isalpha()
True
>>>
12.isdecimal():如果string中只包含十进制数字则返回True,否则返回False。
>>> str4=u"this2015"
>>> print str4.isdecimal()
False
>>> str5=u"12345"
>>> print str5.isdecimal()
True
>>>
13.isdigit():如果字符串中只包含数字则返回True,否则返回False。
>>> str4=u"this2015"
>>> str5=u"12345"
>>> print str4.isdigit()
False
>>> print str5.isdigit()
True
>>>
14.islower():如果字符串中包含至少一个区分大小写的字符,并且所有这些字符都是小写,则返回True,否则返回False。
>>> str4=u"this2015"
>>> str5=u"12345"
>>> print str4.islower()
True
>>> print str5.islower()
False
>>>
15.isspace():如果字符串中只包含空格,则返回True,否则返回False。
>>> >>> str6=""
>>> print str6.isspace()
False
>>> str7=" "
>>> print str7.isspace()
True
>>>
16.istitle():如果是标题化的则返回True,否则返回False。即:字符串中所有的单词首字母是大写,其他都是小写。
>>> str1="This is a string"
>>> str2="This Is A String"
>>> print str1.istitle()
False
>>> print str2.istitle()
True
>>>
17.isupper():字符串中至少一个区分大小写的字符,并且所有这些字符都是大写。则返回True,否则返回False。
>>> str1="This is a string"
>>> str2="This Is A String"
>>> print str2.isupper()
False
>>> str3="THS IS A STRING"
>>> print str3.isupper()
True
>>>