字符串函数

len(x) 长度,返回字符串x的长度

str(x) 任意类型x对应的字符串形式

bin(x) x的二进制小写形式字符串

oct(x) x的八进制小写形式字符串

hex(x) x的十六进制小写形式字符串

chr(u) u为Unicode,返回其对应的字符

ord(x) x为字符,返回其对应的Unicode编码

str.lower() 返回字符串str的副本,所有字符小写    'AsDfGhJkL'.lower() 结果为 'asdfghjkl'

str.upper() 返回字符串str的副本,所有字符大写    'AsDfGhJkL'.upper() 结果为'ASDFGHJKL'

str.split(sep=None) 返回一个列表,由str根据sep被分割的部分组成  print('a,s,d,f,g,h,j,k,l'.split(","))  结果为 ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l']

str.count(sub) 返回子串sub在str中出现的次数    "an apple a day".count("a") 结果为4

str.replace(old,new) 返回字符串str的副本,所有old子串都会被替换成new    "python".replace("n","n123.io") 结果为 "python123.io"

str.center(width[,fillchar]) 字符串str根据宽度width居中,fillchar为可选  "python".center(20,"=") 结果为 "=======python======="

str.strip(char) 从str中去掉其左侧和右侧chars中列出的字符 "=python=".strip("=npt") 结果为 "ytho"    # "t" 还在,理解“去掉其左侧和右侧”

 

字符串的格式化方法

模板字符串.format(逗号分隔的参数)

 

 

 

 

 

 

 

posted on 2018-10-17 18:00  随心动,随风行  阅读(157)  评论(0)    收藏  举报

导航