1.capitalize()

  S.capitalize()->string

 

python的字符串内建函数 python内置字符串_python的字符串内建函数

python的字符串内建函数 python内置字符串_字符串_02

1 str='A222aaA'
2 str.capitalize()#首字母大写,其余字母小写。
3 结果:'A222aaa'

capitalize() eg.

 

2.center()

  S.center(width[,fillchar])->string

  扩充字符串长度,使原字符串居中。

给定字符串长度,如果width小于原字符串长度,则原字符串不变;

给定填充的字符,fillchar默认为空。

python的字符串内建函数 python内置字符串_python的字符串内建函数

center() eg.

 

3.count()

  S.count(sub[,start[,end]])->int

  子字符串在字符串中出现的次数。

给定起始序列号;

给定计数范围的最后一个字符的前一个序列号。

python的字符串内建函数 python内置字符串_python的字符串内建函数

python的字符串内建函数 python内置字符串_字符串_02

1 str='450000045000004500000450000045'
 2 start=0
 3 end=7
 4 sub='45'
 5 str.count(sub,start,end)
 6 结果:1
 7 
 8 str.count(sub,start,8)
 9 结果:1
10 
11 str.count(sub,start,9)
12 结果:2

count() eg.

 

4.decode()

  S.decode([encoding[,errors]])->object

  字符串转码。

给定编码方式,默认为当前默认编码;

给定错误处理方式:

errors='strict'表示抛出UnicodeDecodeError;

errors='ignore',则会忽略非法字符;

errors=replace,则会用?取代非法字符; 
  errors=xmlcharrefreplace,则使用XML的字符引用。

  关于编码详见《python与编码方式》

 

5.encode()

  S.encode([encoding[,errors]])->object

给定编码方式,默认为当前默认编码;

给定错误处理方式:

errors='strict'表示抛出UnicodeEncodeError;

errors='ignore',则会忽略非法字符;

errors=replace,则会用?取代非法字符; 
  errors=xmlcharrefreplace,则使用XML的字符引用。 

  关于编码详见《python与编码方式》

 

6.endswith()  和   startswith()

  S.endswith(suffix[,start[,end]])->bool

  S.startswith(prefix[, start[, end]]) -> bool

/开头。

给定判断所依据的后缀/开头;

给定判断开始序列号;

给定判断范围的最后一个字符的前一个序列号。

python的字符串内建函数 python内置字符串_python的字符串内建函数

python的字符串内建函数 python内置字符串_字符串_02

1 str='1234567890'
 2 suffix='4'
 3 start=1
 4 end=4
 5 str.endswith(suffix,start,end)#查找‘234’里是否以‘4’结尾
 6 结果:True
 7 
 8 end=3
 9 str.endswith(suffix,start,end)#查找‘23’里是否以‘4’结尾
10 结果:False

endswith() eg.

python的字符串内建函数 python内置字符串_python的字符串内建函数

python的字符串内建函数 python内置字符串_字符串_02

1 str='1234567890'
 2 suffix='2'
 3 start=1
 4 end=4
 5 str.startswith(suffix,start,end)#查找‘234’里是否以‘2’开头
 6 结果:True
 7  
 8 start=0
 9 str.startswith(suffix,start,end)#查找‘1234’里是否以‘2’开头
10 结果:False

startswith() eg.

 

7.expandtabs()

  S.expandtabs([tabsize])->string

/t替换为空格。

给定每个/t替换多少个空格,默认为8

  

python的字符串内建函数 python内置字符串_字符串长度_10

 

8.find()  和  rfind()

  S.find(sub[,start[,end]])  ->int

  S.rfind(sub[,start[,end]])  ->int

/右开始,子字符串查找。

-1

  

python的字符串内建函数 python内置字符串_字符串长度_11

 

9.format()

  S.format(*args,**kwargs)->string

  字符串格式化输出。

*args无key值,**kwargs有key值

  关于字符串格式化详见《python字符串格式化》

 

10.index()  和   rindex()

  S.index(sub[,start[,end]])  ->int

  S.rindex(sub[,start[,end]])  ->int

find()

 

11.isalnum()

  S.isalnum()->bool

  判断字符串中是否全为数字和字母,且字符串不为空。

  

python的字符串内建函数 python内置字符串_ico_12

 

12.isalpha()

  S.isalpha()->bool

  判断字符串是否全是字母。

  

python的字符串内建函数 python内置字符串_python的字符串内建函数_13

 

13.isdigit()

  S.isdigit()->bool

  判断字符串是否全是数字。

  

python的字符串内建函数 python内置字符串_python的字符串内建函数_14

 

14.islower()  和  isupper()

  S.islower()->bool

  S.isupper()->bool

/大写字母。 

  

python的字符串内建函数 python内置字符串_ico_15

 

15.isspace()

  S.isspace()->bool

  判断字符串中是否有空格。

  

python的字符串内建函数 python内置字符串_字符串长度_16

 

16.istitle()

  S.istitle()->bool

  判断字符串是否只有首字母大写。

  

python的字符串内建函数 python内置字符串_字符串_17

 

17.join()

  S.join(iterable)->string

  将字符串作为可迭代对象中字符串类型元素的分割符,并连接各元素。

给定元素为字符串的可迭代对象

  

python的字符串内建函数 python内置字符串_字符串长度_18

 

18.ljust()  和  rjust()

  S.ljust(width[,fillchar])->string

  S.rjust(width[,fillchar])->string

/右。

给定字符串长度,如果width小于原字符串长度,则原字符串不变;

给定填充的字符,fillchar默认为空。

  

python的字符串内建函数 python内置字符串_python的字符串内建函数_19

 

19.lower()  和  upper()

  S.lower()->string

  S.upper()->string

/小写字母变为大写。

  

python的字符串内建函数 python内置字符串_python的字符串内建函数_20

 

20.partition()  和  rpartition()

  S.partition(sep)->(head,sep,tail)

  S.rpartition(sep)->(head,sep,tail)

/右边第一个给定的分割符,并按照(分割符前,分割符,分割符后)的格式返回元组。

给定分割符

2个空字符串组成的元组。

  

python的字符串内建函数 python内置字符串_ico_21

 

21.replace()

  S.replace(old,new[,count])->string

  字符串替换。

count,则给定替换次数

  

python的字符串内建函数 python内置字符串_python的字符串内建函数_22

 

22.split()  和  rsplit()

  S.split([sep[,maxsplit]])->list of strings

/右边对字符串拆分。

给定分割符;

给定拆分到第几个分割符

  

python的字符串内建函数 python内置字符串_ico_23

 

23.splitlines()

  S.splitlines(keepends=False) -> list of strings

按行分割字符串。

默认为False,不保留换行符;若为True则保留换行符

  

python的字符串内建函数 python内置字符串_python的字符串内建函数_24

 

24.strip()

  S.strip([chars])->string or unicode

  去除字符串开头和结尾中的给定字符。

给定要去掉的字符,如果chars为unicode,则先将字符串转换为unicode

  

python的字符串内建函数 python内置字符串_python的字符串内建函数_25

 

25.lstrip()  和  rstrip()

  S.lstrip([chars])->string or unicode

/右边给定字符。

给定要去掉的字符,如果chars为unicode,则先将字符串转换为unicode

  

python的字符串内建函数 python内置字符串_字符串长度_26

 

26.swapcase()

  S.swapcase()->string 

  交换大小写。

  

python的字符串内建函数 python内置字符串_字符串长度_27

 

27.title()

  S.title()->string

  首字母大写。

  

python的字符串内建函数 python内置字符串_字符串_28

 

28.translate()

  S.translate(table [,deletechars]) -> string

 

29.zfill()

  S.zfill(width) -> string

0填充

  

python的字符串内建函数 python内置字符串_ico_29