python数据类型--字符串(一)

一、字符串之capitalize

text = "abcd"
v = text.capitalize()
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
Abcd

Process finished with exit code 0

参数:capitalize(self)

总结:capitalize将英文首字母改为大写字母

二、字符串之casefold

text = 'ABCcd'
v = text.casefold()
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
abccd

Process finished with exit code 0

参数: casefold(self)

总结:casefold将字符串中的大写字母转换为小写字母。

三、字符串之center

示例1:

text = 'ABCcd'
v = text.center(11)
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
   ABCcd   

Process finished with exit code 0

示例2:

text = 'ABCcd'
v = text.center(11,"*")
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
***ABCcd***

Process finished with exit code 0

参数:center(self,width,fillchar=None)

总结:从以上两个示例中来看,字符串以center第一个(整型)参数为字符串长度进行居中,其它位默认以空格填充,如指定第二个参数(字符串参数),则以第二个参数填充。

四、字符串之count

示例1:

text = 'ABCcdcecd'
v = text.count('c')
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
3

Process finished with exit code 0

示例2:

text = 'ABCcdcecd'
v = text.count('c',5)
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
2

Process finished with exit code 0

示例3:

text = 'ABCcdcecd'
v = text.count('c',5,7)
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
1

Process finished with exit code 0

参数:count(self,sub,start=None,end=None)

总结:从以上三个示例中看,count是查找字符串中存在第一个参数(区分大小写)的个数,第二个参数为查找的起始位置,第三个参数为查找的结束位置。

五、字符串之endswith

示例1:

text = 'ABCcdcecd'
v = text.endswith('cd')
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
True

Process finished with exit code 0

示例2:

text = 'ABCcdcecd'
v = text.endswith('cd',3,5)
print(v)

输出:

"D:\python\oldboy study\venv\Scripts\python.exe" "D:/python/oldboy study/S1.py"
True

Process finished with exit code 0

参数:endswith(self,suffix,start=None,end=None)

总结:从以上两个示例中看,endswith是查看字符串是否以第一个参数值为结尾,第二个参数为查找的起始位置,第三个参数为查找的结束位置。如果为真返回True,如果为假返回False。


备注:在参数中,如果参数赋值为None,或指定值,则在调用对象时如果不进行赋值,则使用默认值。


二〇一八年六月十一日 22:58:28




点赞