字符串操作

字符串特性:不可修改
字符串名:name="liuhailin"
字符串名:name="{liu}hai{lin}"
name.capitalize() 首字母大写
name.count("a") 统计字符串a 有几个
name.center(50,"-") 打印50字符,不够用-补上。字符串在中间
name.endswith("ex") 判断是不是以ex结尾
name.expandtabs(tabsize=30) 吧tab键转换成多少个空格30个
name.find("hai") 获取字符串索引下标,找不到返回-1 
name.format(liu=1,lin="ge") 格式化字符串
name.islnum() 判断包含阿拉伯数字,不能有特殊字符
name.isalpha() 判断纯英文字符
name.isdecimal() 判断是否十进制
name.isdigit()判断是不是整数
name.isidentifier()判断是不是一个合法的标识符(变量名)
name.islower() 判断是不是一个小写字符
name.isspace()判断是不是一个空格
name.istitle()判断每个字符首字母是不是大写
name.isprintable() 判断是不是可以打印出来
name.isupper() 判断是不是大写字符
'+'.join(['1','2','3'])  把数字用+符号链接起来变成一个字符串。如:1+2+3
name.ljust(50,'*') 打印50个字符,不够用*号向右补上
name.rjust(50,'*') 打印50个字符,不够用*号向左补上
name.lower() 吧大写变成小写
name.upper() 吧小写变成大写
' Alex'.lstrip() 去掉左边的空格
'Alex '.lstrip() 去掉右边的空格
'     alex '.strip() 去掉全部空格
 
p=str.maketrans("abcdef",'123456') 
"alex li".translate(p)   把字符对应相应的数字。然后把对应的字符替换成数字
'alex li '.replace('l','L',1) 把第一个l替换成大写的L
'alex li'.rfind('l')从左往右找到l最后一个的下标
'alex li'.split() 把字符分成列表,默认把空格当成分隔符
'1+2 +3+4'.splitlines() 把字符分成列表,默认把空格当成分隔符.与上面不同的是可以自动识别不同系统的换行符或其他符号。
'alex li'.swapcase() 大小写互换
'alex li'.zfill(50) 打印50字符,不够从左往右用0补上
 
input接受字符串时,如何转换成字典数据类型:
b=''' 
  'recored':{
       'server':'127.0.0.1'
        'weight':20
        'maxconn':30
'''
eval(b)
 
字符串格式化;
%c:单个字符
%d:十进制整数
%o:八进制整数
%s:字符串
%x:十六进制整数,其中的字母小写
%X:十六进制整数,其中的字母大写
 
中文字符串处理:
encoding默认编码处理方式:utf-8,也可以使用gbk,gb2312 等方式编码
errors编码错误的处理方式,默认为strict(报错),也可以是ignore,replace等方式
反之,如果从网络上接受的字节串(bytes)若为字符串,则要使用字节串(bytes)的decode()方法解码。
decode(encodeing='utf-8',errors='strict')
 
name.capitalize()  首字母大写
name.casefold()   大写全部变小写
name.center(50,"-")  输出 '---------------------Alex Li----------------------'
name.count('lex') 统计 lex出现次数
name.encode()  将字符串编码成bytes格式
name.endswith("Li")  判断字符串是否以 Li结尾
 "Alex	Li".expandtabs(10) 输出'Alex      Li', 将	转换成多长的空格 
 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
原文地址:https://www.cnblogs.com/xuepython/p/6625911.html