继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

python string

英明神武的牛
关注TA
已关注
手记 317
粉丝 30
获赞 175

方法  描述
string.ljust(width)
返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
\>>> x.ljust(20)
'Hello,wold       ‘

string.center(width)
返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
\>>> x.center(20)
'     Hello,wold     '
\>>>
string.rjust(width)
返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
\>>> x.rjust(20)
'          Hello,wold'

string.zfill(width)
返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0
\>>> str.zfill(20)
'000Book on the deskk'
\>>> str.zfill(30)
'0000000000000Book on the deskk'

string.startswith(obj, beg=0,end=len(string))
检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查.

string.endswith(obj, beg=0, end=len(string))
检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.

string.isalnum()
isalnum() 方法检测字符串是否由字母和数字组成。
如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
\>>> str="this2010"
\>>> str.isalnum()
True
\>>> str="this2010."
\>>> str.isalnum()
False
\>>> str="this"
\>>> str.isalnum()
True
\>>> str="123456"
\>>> str.isalnum()
True
\>>>

string.isalpha()
如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
\>>> str="this"
\>>> str.isalpha()
True
\>>> str.isalpha()
False
\>>>

string.isdigit()
如果 string 只包含数字则返回 True 否则返回 False.
\>>> a=2
\>>> a.isdigit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'isdigit'
\>>> str="123"
\>>> str.isdigit()
True
\>>> str="this is a example"
\>>> str.isdigit()
False
\>>> string.isdecimal()
isdecimal()方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
\>>> str=u"1234"
\>>> str.isdecimal()
True
\>>> str=u"this2010"
\>>> str.isdecimal()
False
\>>>
string.islower()
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
\>>> str='i89ke'
\>>> str.islower()
True

string.isupper()
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False

string.isnumeric()
如果 string 中只包含数字字符,则返回 True,否则返回 False
\>>> str=u'234'
\>>> str.isnumeric()
True
\>>> str=u'this2010'
\>>> str.isnumeric()
False
\>>>

string.isspace()
如果 string 中只包含空格,则返回 True,否则返回 False.
\>>> str="    "                            #至少包含一个以上空格
\>>> str.isspace()
True
\>>> str=""
\>>> str.isspace()
False
\>>>

string.istitle()
如果字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False.
\>>> str="Dont Do It"
\>>> str.istitle()
True
\>>> str="Dont do it"
\>>> str.istitle()
False
\>>>

string.capitalize()
把字符串的第一个字符大写
\>>> s='a,B'
\>>> s.capitalize()
'A,b'
\>>> str="this is very interintsting"
\>>> str.capitalize()
'This is very interintsting'
\>>>
string.lower()
转换 string 中所有大写字符为小写.
\>>> s.lower()
'a,b'

string.upper()
转换 string 中的小写字母为大写
\>>> s.upper()
'A,B'
\>>>

string.title()
返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
\>>> str="This a book,And that?"
\>>> str.title()
'This A Book,And That?'

max(str)
返回字符串 str 中最大的字母。
min(str)
返回字符串 str 中最小的字母。
\>>> str="this is really a string example....wow!!"
\>>> max(str)
'y'
\>>> min(str)
' '
\>>> len(str)
40

string.format()
格式化字符串

string.count(str, beg=0, end=len(string))
返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
\>>> str="this is string example....wow!!"
\>>> sub='i'
\>>> str.count(sub,4,40)
2
\>>> str.count('wow')
1

string.replace(str1, str2,  num=string.count(str1))
把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次.
返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。
\>>> str="this is string example...wow!! this is really string"
\>>> str.replace('is','wa')
'thwa wa string example...wow!! thwa wa really string'
\>>> str.replace('is','wa','2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
\>>> str.replace('is','was',2)
'thwas was string example...wow!! this is really string'
\>>>

string.rindex( str, beg=0,end=len(string))
类似于 index(),不过是从右边开始.
string.index(str, beg=0, end=len(string))
跟find()方法一样,只不过如果str不在 string中会报一个异常.
\>>> str="this is string example...woow!!"
\>>> str2="exam"
\>>> str.index(str2)
15
\>>> str.index(str2,14)
15
\>>> str.index(str2,16)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
\>>>

string.rfind(str, beg=0,end=len(string) )
类似于 find()函数,不过是从右边开始查找.
string.find(str, beg=0, end=len(string))
检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。
\>>> str="this is string example...woow!!"
\>>> str2="exam"
\>>> str.find(str2)
15
\>>> str.find(str2,10)
15
\>>> str.find(str2,50)
-1

string.split(str="", num=string.count(str))
以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串

string.splitlines([keepends])
按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

string.join(seq)
以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
\>>> str="-"
\>>> seq=['a','b','c']
\>>> str.join(seq)
'a-b-c'
\>>> str='8080'
\>>> str.join(seq)
'a8080b8080c'
\>>>

string.rpartition(str)
类似于 partition()函数,不过是从右边开始查找.
string.partition(str)
有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 元 素 的 元 组 (string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str == string.
返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串
\>>> str="this is very interintsting"
\>>> str.capitalize()
'This is very interintsting'
\>>> x="is"
\>>> str.partition(x)
('th', 'is', ' is very interintsting')
\>>> str.rpartition(x)
('this ', 'is', ' very interintsting')

string.strip([obj])
在 string 上执行 lstrip()和 rstrip()
\>>> str="9999what8888"
\>>> str.strip('9')
'what8888'
\>>> str.strip('8')
'9999what'
\>>> str.strip('9''8')
'what'
\>>> str.strip('8''9')
'what'

string.lstrip()
截掉 string 左边的空格
\>>> str="9999what8888"
\>>> str.lstrip('9''w')
'hat8888'

string.rstrip()
删除 string 字符串末尾的空格.
\>>> str="9999what8888"
\>>> str.rstrip('8''t')
'9999wha'

string.swapcase()
翻转 string 中的大小写
\>>> str="This a book,And that?"
\>>> str.swapcase()
'tHIS A BOOK,aND THAT?'

string.translate(str, del="")
根据 str 给出的表(包含 256 个字符)转换 string 的字符,
要过滤掉的字符放到 del 参数中

string.maketrans(intab, outtab])
maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。

string.expandtabs(tabsize=8)
把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8。

string.decode(encoding='UTF-8', errors='strict')
以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除非 errors 指 定 的 是 'ignore' 或 者'replace'

string.encode(encoding='UTF-8', errors='strict')
以 encoding 指定的编码格式编码 string,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace'

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP