对字符串的处理 - liudl240/PYTHON3 GitHub Wiki
string.capitalize() #将打一个字母变为大写
string1="james liu"
print(string1.capitalize())
# James liu
字符串拼接
string1="hi,james liu"
print(string1[:3] + "zhangsan")
# hi,zhangsan
\t 横向制表符
string1="hi\tjames\tliu"
print(string1)
# hi james liu
\v 纵向制表符
in | not in
string1="james liu"
if "james" in string1:
print ("True")
else:
print ("False")
if "123" not in string1:
print ("True")
else:
print ("False")
# True
# True
center(width, fillchar) #返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。
string1="james liu"
print(string1.center(50,"+"))
# ++++++++++++++++++++james liu+++++++++++++++++++++
count(str, beg= 0,end=len(string)) #返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
string1="hi,james liu"
print(string1.count("i",1,5))
print(string1.count("i",1))
# 1
# 2
endswith(suffix, beg=0, end=len(string)) #检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False
string1="hi,james liu"
print(string1.endswith("liu"))
print(string1.endswith("james"))
print(string1.endswith("james",0,8))
#True
#False
#True
find(str, beg=0 end=len(string)) #检测 str 是否包含在字符串中,如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1
string1="hi,james liu"
print(string1.find("i"))
print(string1.rfind("i"))
print(string1.rfind("0"))
# 1
# 10
# -1
index(str, beg=0, end=len(string)) #跟find()方法一样,只不过如果str不在字符串中会报一个异常.
string1="hi,james liu"
print(string1.index("j"))
print(string1.index("0"))
3
Traceback (most recent call last):
File "D:/python_james/test02/test01.py", line 32, in <module>
print(string1.index("0"))
ValueError: substring not found
isalnum() #如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False
string1="hi,james liu"
print(string1.isalnum())
string1="a12"
print(string1.isalnum())
string1=","
print(string1.isalnum())
#False
#True
#False
isalpha() #如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False
info="hi,james liu"
print (info.isalpha())
info="james"
print (info.isalpha())
#False
#True
isdigit() #如果字符串只包含数字则返回 True 否则返回 False
info="hi,james liu"
print (info.isdigit())
info="123"
print (info.isdigit())
#False
#True
islower() #如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
info="hi,James liu"
print (info.islower())
info="james"
print (info.islower())
#False
#True
isnumeric() #如果字符串中只包含数字字符,则返回 True,否则返回 False
info="hi,James liu"
print (info.isnumeric())
info="12"
print (info.isnumeric())
#False
#True
isspace() #如果字符串中只包含空白,则返回 True,否则返回 False
info="hi,James liu"
print (info.isspace())
info=" "
print (info.isspace())
#False
#True
istitle() #如果字符串是标题化的(见 title())则返回 True,否则返回 False
title 单词首字母大写
info="hi,James liu"
print (info.istitle())
info="Hi,James Liu"
print (info.istitle())
#False
#True
isupper() #如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
info="hi,James"
print (info.istitle())
info="Hi,James"
print (info.istitle())
#False
#True
join(seq) #以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
s1 = "-"
s2 = " "
seq = ("hi","james","liu",".") # 字符串序列
print (s1.join( seq ))
print (s2.join( seq ))
#hi-james-liu-.
#hi james liu .
len(string) #返回字符串长度
info="hi,James"
print(len(info))
#8
ljust(width[, fillchar]) #返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格。
info="""hi,james,liu"""
print(info.ljust(50,"#"))
#hi,james,liu######################################
lower() #转换字符串中所有大写字符为小写
info="""HI,JAMES LIU"""
print(info.lower())
#hi,james liu
lstrip() #截掉字符串左边的空格或指定字符
info = """ hi, james liu"""
print(info.lstrip())
print(info.lstrip("hi,"))
print(info.lstrip(" hi,"))
#hi, james liu
# hi, james liu
#james liu
max(str) #返回字符串 str 中最大的字母。
info="""hi,james,liu2131"""
print(max(info))
#u
min(str) #返回字符串 str 中最小的字母
info="""hi,james,liu2131"""
print(max(info))
print(min(info))
#u
#,
replace(old, new [, max]) #把 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次
info="""hi,james liu,are you james liu?"""
print(info.replace("james liu","zhangsan",1))
print(info.replace("james liu","zhangsan"))
#hi,zhangsan,are you james liu?
#hi,zhangsan,are you zhangsan?
rfind(str, beg=0,end=len(string)) #类似于 find()函数,不过是从右边开始查找
info="""hi,james liu,are you james liu?"""
print(info.rfind("liu"))
print(info.rfind("liu",0,20))
print(info.rfind("zhangsan"))
#27
#9
#-1
rindex( str, beg=0, end=len(string)) #类似于 index(),不过是从右边开始
info="""hi,james liu,are you james liu?"""
print(info.rindex("liu"))
print(info.rindex("liu",0,20))
print(info.rindex("zhangs"))
#27
#Traceback (most recent call last):
#9
# File "D:/python2/day001/test01.py", line 38, in <module>
# print(info.rindex("zhangs"))
#ValueError: substring not found
rjust(width,[, fillchar]) #返回一个原字符串右对齐,并使用fillchar(默认空格)填充至长度 width 的新字符串
info = """ hi, james liu"""
print(info.rjust(50,"#"))
################################# hi, james liu
rstrip() #删除字符串字符串末尾的空格
info = """ hi, james liu """
print(info)
print(info.rstrip())
# hi, james liu
# hi, james liu
split(str="", num=string.count(str)) #num=string.count(str)) 以 str 为分隔符截取字符串,如果 num 有指定值,则仅截取 num+1 个子字符串
info = """ hi james liu ,are you ok"""
print(info.split(" "))
print(info.split(","))
#['', 'hi', 'james', 'liu', ',are', 'you', 'ok']
#[' hi james liu ', 'are you ok']
splitlines([keepends]) #按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
info="""hi\n
james liu \n
are you ok?\n"""
print(info)
print(info.splitlines())
#hi
#
#james liu
#3
#are you ok?
#
#['hi', '', 'james liu ', '', 'are you ok?']
startswith(substr, beg=0,end=len(string)) #检查字符串是否是以指定子字符串 substr 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查
info="hi,james,liu"
print(info.startswith("hi"))
print(info.startswith("abc"))
print(info.startswith("james",3))
#True
#False
#True
strip([chars]) #在字符串上执行 lstrip()和 rstrip()
info="""******************hi , james liu**************"""
print(info.strip())
print(info.strip("*"))
#******************hi , james liu**************
#hi , james liu
swapcase() #将字符串中大写转换为小写,小写转换为大写
info="""Hi,James Liu"""
print(info.swapcase())
#hI,jAMES lIU
title() #返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
info="hi,james,liu"
print(info.title())
print(info.title().istitle())
#Hi,James,Liu
#True
translate(table, deletechars="") #根据 str 给出的表(包含 256 个字符)转换 string 的字符, 要过滤掉的字符放到 deletechars 参数中
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab) # 制作翻译表
str = "this is string example....wow!!!"
print(str.translate(trantab))
#th3s 3s str3ng 2x1mpl2....w4w!!!
upper() #转换字符串中的小写字母为大写
info="""hi ,james liu"""
print(info.upper())
#HI ,JAMES LIU
zfill (width) #返回长度为 width 的字符串,原字符串右对齐,前面填充0
info="""hi ,james liu"""
print(info.zfill(50))
#0000000000000000000000000000000000000hi ,james liu
isdecimal() 检查字符串是否只包含十进制字符,如果是返回 true,否则返回 false
info="""hi ,james liu"""
print(info.isdecimal())
info="10"
print(info.isdecimal())
#False
#True