字符串函数
#capitalize()将字符串第一个字符转换为大写
print(str.capitalize("ajhak"))
-------------------------- 运行结果 --------------------------
Ajhak
------------------------------------------------------------------
#center(width,fillchar) 返回指定的宽度width。fillchar填充的字符,默认为空格
a="ahsjfghaj"
print(a.center(50,"-"))
-------------------------- 运行结果 --------------------------
--------------------ahsjfghaj---------------------
------------------------------------------------------------------
#bytes.decode() 语法:bytes.decode(encodeing="utf-8",errors="strict")
str="基础学习"
str_utf8=str.encode("utf-8")
print("utf-8: ",str_utf8.decode("utf-8","strict"))
-------------------------- 运行结果 --------------------------
utf-8: 基础学习
------------------------------------------------------------------
#count(str,beg=0,end=len(string)) 返回str在string里面出现的次数,如果beg或者end指定则返回指定范围内str出现的次数
ocunt="dahdsssssdd"
print(ocunt.count("s",0,10))
-------------------------- 运行结果 --------------------------
5
------------------------------------------------------------------
#endswith(suffix,beg=0,end=len(string))检查字符串是否以obj结束,如果beg或指定则检查指定的范围内是否以obj结束,如果是返回true,否则false
#简单理解:如果字符串含有指定的后缀返回true,否则返回为false
endswith="asdhnfkasjdhfkahjs"
print(endswith.endswith("ajs"))
-------------------------- 运行结果 --------------------------
False
------------------------------------------------------------------
#expandtabs(tabsize=8)把字符串string中的tab符号转换为空格,tab符号默认的空格数是8
expandtabs="sjaskjdhf asdf"
print(expandtabs.expandtabs()) #默认8个空格
print(expandtabs.expandtabs(tabsize=50)) #指定了50个
-------------------------- 运行结果 --------------------------
sjaskjdhf asdf
sjaskjdhf asdf
------------------------------------------------------------------
#find(str,beg=0,end=len(string))包含子字符串返回开始的索引值,否则返回-1。
finds="ashjahsxxas"
print(finds.find("sxx",0,10))
print(finds.find("sxx",0,5))
-------------------------- 运行结果 --------------------------
6
-1
------------------------------------------------------------------
#index(str,beg=0,end=len(string))同find()没有检测到的就报异常
indexs="jahsdfkjashd"
print(indexs.index("h",0,11))
-------------------------- 运行结果 --------------------------
2
------------------------------------------------------------------
#isalnum()如果字符串至少有一个字符并且所有的字符都是字母或数字则返回为true,否则返回False
print("asfas".isalnum()) --------------- True
#isalpha()如果字符串至少有一个字符并且所有字符都是字母则返回为true,否则返回flase
print("asfas121".isalpha()) --------------- False
#isdigit()如果字符串中只包含数字返回true,否则返回flase
#islower()字符串中都是小写返回true,否则为flase
#ispace()只包含空格返回true,否则返回flase
#istitle()如果字符串标题化的返回true,否则flase
#isupper()字符串都为大写返回true,否则返回为flase
#jion(seq)指定字符串为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串
s1="-"
s2=""
seq=("s","t","u","d","y")
print(s1.join(seq))
print(s2.join(seq))
-------------------------- 运行结果 --------------------------
------------------------------------------------------------------
#len(string)返回字符长度
print(len("asasdfkj"))
-------------------------- 运行结果 --------------------------
------------------------------------------------------------------
#ljust(width,fillchar) 返回一个原字符串对齐。并使用fillshar填充至长度width的新字符串,fillchar默认为空格
print("sda".ljust(50,"^"))
-------------------------- 运行结果 --------------------------
sda^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
------------------------------------------------------------------
#lstrip()返回截掉字符串左边的空格或指定字符后生成的新字符
lstrips=" This is string example......wow!!!"
print(lstrips.lstrip())
-------------------------- 运行结果 --------------------------
This is string example......wow!!!
------------------------------------------------------------------
lstripss="----------This is string example......wow!!!----------"
print(lstripss.lstrip("-"))
-------------------------- 运行结果 --------------------------
This is string example......wow!!!----------
------------------------------------------------------------------
#maketrans() 语法:str.maketrans(intab,outtab)返回字符串转换后生成的新字符串
#max(str)返回字符串str中最大的字母
print(max("afafadsf")) -------------------- s
#min(str)返回字符串str中最小的字母
print(min("asghdfgaghfasd")) ---------------- a
#replace(old,new,max) 把字符串中的str1替换成str2,如果指定第三个参数max。则替换不超过max次
print("ahjgfahjabhj".replace("hj","yu",2)) ----------- ayugfayuasdhj
#rfind()返回字符串最后一次出现的位置,如果没有匹配项则返回-1
rfinds01="This is really a string example...wow!!!"
rfinds02="is"
print(rfinds01.rfind(rfinds02))
-------------------------- 运行结果 --------------------------
5
------------------------------------------------------------------
#rstrip()删除字符串末尾的空格
print("ksdsdfasd ".rstrip()) ------------- ksdsdfasd
#split()通过指定分隔符对字符串进行切片,如果第二个参数num指定值,则分割为num+1个子字符串
url = "http://www.baidu.com/python/image/123456.jpg"
print(url.split("."))
print(url.split("/")[5:6])
-------------------------- 运行结果 --------------------------
['http://www', 'baidu', 'com/python/image/123456', 'jpg']
['123456.jpg']
------------------------------------------------------------------
#strip()用于移除字符串头尾指定的字符(默认为空格)或字符序列;注意:该方法只能删除头尾的字符串,不能删除中间的字符串
print(" ' ' ' 'asdfhajsdg' ' ' ' ".strip(" ' ")) ----------- asdfhajsdg
#swapcase()将字符串中小写转为大写,大写转小写
print("sdfaASDFjd".swapcase()) ------------- SDFAasdfJD