python-字符串操作

特性:不可修改 

字符串操作方法:

 s.captitalize()                                  首字母变大写

s.center(width  [, pad ] )                   在长度为width的字段内将字符串居中,pad是填充字符

s.count (sub [, start [ ,end ] ] )        计算指定子字符串sub的出现次数

s.decode( [encoding  [, errors]])        解码一个字符串并返回一个Unicode字符串(只能用于字节字符串)

s.encode( [encoding [,errors] ])         返回字符串的编码版本(只能用于Unicode字符串)

s.endswith(suffix [,start [,end]])        检查字符串是否以suffix为结尾

s.expandtabs([tabsize])                    使用空格替换制表符

s.find(sub [ ,start [, end]])               找到指定子字符串sub首次出现的位置,否则返回-1

s.format(*args ,**kwargs)               格式化s

s.index(sub [, start [,end]])             找到指定字符串sub首次出现的位置,否则报错

s.isalnum()                                     检查所有字符串是否都为字母或者数字

s.isalpha()                                      检查所有字符串是否都为字母

s.isdigit()                                       检查所有字符串是否都为数字

s.islower()                                      检查所有字符串是否都为小写

s.isspace()                                      检查所有字符串是否都为空白

s.isupper()                                     检查所有字符串是否都为大写

s.join(t)                                         连接序列t中的字符串

s.ljust(width [, fill])                         在长度为width的字符串内左对齐s

s.lower()                                        转换为小写形式

s.lstrip([chars])                              删掉 chars前面的空白或字符

s.partition(sep)                               使用分隔符字符串sep划分一个字符串,返回一个元组(head,sep,tail),如果未找到,则返回(s,“”,“”)

s.replace(old ,new [,maxreplace])      替换一个子字符串

s.rfind(sub [,start [,end]])               找到一个子字符串的最后一次出现的位置

s.rindex(sub,[start, [,end])              找到一个子字符串最后一次出现的位置,否则报错

s.rjust(width [,fill])                          在长度为width的字符串内右对齐s

s.rpartition(sep)                              使用分隔符字符串sep划分字符串s,但是从字符串的结尾处开始搜索

name.capitalize()  首字母大写
name.casefold()   大写全部变小写
name.center(50,"-")  输出 '---------------------Alex Li----------------------'
name.count('lex') 统计 lex出现次数
name.encode()  将字符串编码成bytes格式
name.endswith("Li")  判断字符串是否以 Li结尾
 "Alex\tLi".expandtabs(10) 输出'Alex      Li', 将\t转换成多长的空格 
 name.find('A')  查找A,找到返回其索引, 找不到返回-1 

format :
    >>> msg = "my name is {}, and age is {}"
    >>> msg.format("alex",22)
    'my name is alex, and age is 22'
    >>> msg = "my name is {1}, and age is {0}"
    >>> msg.format("alex",22)
    'my name is 22, and age is alex'
    >>> msg = "my name is {name}, and age is {age}"
    >>> msg.format(age=22,name="ale")
    'my name is ale, and age is 22'
format_map
    >>> msg.format_map({'name':'alex','age':22})
    'my name is alex, and age is 22'


msg.index('a')  返回a所在字符串的索引
'9aA'.isalnum()   True

'9'.isdigit() 是否整数
name.isnumeric  
name.isprintable
name.isspace
name.istitle
name.isupper
 "|".join(['alex','jack','rain'])
'alex|jack|rain'


maketrans
    >>> intab = "aeiou"  #This is the string having actual characters. 
    >>> outtab = "12345" #This is the string having corresponding mapping character
    >>> trantab = str.maketrans(intab, outtab)
    >>> 
    >>> str = "this is string example....wow!!!"
    >>> str.translate(trantab)
    'th3s 3s str3ng 2x1mpl2....w4w!!!'

 msg.partition('is')   输出 ('my name ', 'is', ' {name}, and age is {age}') 

 >>> "alex li, chinese name is lijie".replace("li","LI",1)
     'alex LI, chinese name is lijie'

 msg.swapcase 大小写互换


 >>> msg.zfill(40)
'00000my name is {name}, and age is {age}'



>>> n4.ljust(40,"-")
'Hello 2orld-----------------------------'
>>> n4.rjust(40,"-")
'-----------------------------Hello 2orld'


>>> b="ddefdsdff_哈哈" 
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True

猜你喜欢

转载自www.cnblogs.com/fsllovexzd/p/10604769.html