字符串操作与内置函数
capitalize 函数
capitalize 功能
首字母大写,其他字母小写
capitalize 用法
用法:newstr = string.capitalize()
参数:函数括弧内什么都不用填写
name = "chiNa"
new_name = name.capitalize()
print(name)
# 打印结果
chiNa
capitalize 注意事项
只对第一个字母大写有效
只对字母有效
已经是大写则无效
# 错误的使用方法
number = '1ok'
lange = "你好abc"
had = "Good"
字符串小写函数
caseflod 与 lower 功能
将字符串全体转换成小写
caseflod 与 lower 用法
newstr = string.casefold() > 函数括弧内什么都不用填写
newstr = string.lower() > 函数括弧内什么都不用填写
name = "CHINA"
new_name = name.lower()
print(new_name)
# 打印结果
china
caseflod 与 lower 注意事项
只对字符串中的字母有效
字符串已经是小写,则无效
# 错误的使用方法
number = '1ok'
lange = "你好abc"
had = "good"
caseflod 和 lower的区别
对 Unicode 的时候用 casefold
lower() 只对 ASCII 也就是 'A-Z'有效,但是其它一些语言里面存在小写的情况就没办法了。
汉语 & 英语环境下面,继续用 lower()没问题;要处理其它语言且存在大小写情况的时候再用casefold()
upper函数
upper 功能
将字符串全体转换成大写
upper 用法
big_str = string.upper() > 函数括弧内什么都不用填写
name = 'china'
big_name = name.upper()
print(big_name)
# 打印结果
CHINA
upper 注意事项
只对字符串中的字母有效
已经是大写,则无效
# 错误的使用方法
number = '1ok'
lange = "你好abc"
had = "good"
swapcase 函数
swapcase 功能
将字符串中大小写字母进行转换
swapcase 用法
newstr = string.swapcase() > 函数括弧内什么都不用填写
name = 'chINa'
new_name = name.swapcase()
print(new_name)
# 打印结果
CHinA
swapcase 注意事项
只对字符串的字母有效
number = '[email protected]' -> '[email protected]
info = 'python语言
zfill 函数
zfill 功能
为字符串定义长度,如不满足,缺少的部分用 0 填补
zfill 用法
newstr = string.zfill(width) > width: 新字符串希望的宽度
name = 'hello word'
new_str = name.zfill(15)
print(new_str)
# 打印结果
00000hello word
zfill 注意事项
与字符串的字符无关
如果定义长度小于当前字符串长度,则不发生变化
name = 'my name is tom'
new_str = name.zfill(10)
print(new_str)
# 打印结果
my name is tom
count 函数
count 功能
返回当前字符串中某个成员(元素)的个数
count 用法
string.count(sub) > 括弧里需要传一个你想查询个数的元素, 返回一个整数
name = 'hello word'
new_str = name.count('l')
print(new_str)
# 打印结果
2
count 注意事项
如果查询的成员(元素)不存在,则返回0
count 函数可以限制字符串的字段
test_str = 'my name is tom'
count = test_str.count('b')
print(count) -> 0
startwith 和 endswith 函数
startwith 和 endswith 功能
startswith 判断字符串开始是否是某成员(元素)
endswith 判断字符串结尾是否是某成员(元素)
startwith 和 endswith 用法
string.startswith(sub) -> sub:你想查询匹配的元素, 返回一个布尔值
string.endswith(sub) -> sub你想查询匹配的元素, 返回一个布尔值
# stratswith
str_name = 'hello word'
new_str = str_name.startswith("h")
print(new_str)
# 打印结果
True
# denswith
str_name = 'hello word'
new_str = str_name.endswith("h")
print(new_str)
# 打印结果
False
find 与 index 函数
find 与 index 都是返回你想寻找的成员的位置
find 与 index 功能
string.find(sub) -> sub:你想查询的元素, 返回一个整型
string.index(sub) -> sub:你想查询的元素, 返回一个整型
Ps:字符串里的位置是从0开始的
find 与 index 用法
# find
str_name = "hello word!"
new_str = str_name.find('e')
print(new_str)
# 打印结果
1
# index
str_name = "hello word!"
new_str = str_name.index('l')
print(new_str)
# 打印结果
2
find 与 index 的区别
如果find找不到元素,会返回-1
如果index 找不到元素,会导致程序报错
strip 函数
strip 功能
strip 将去掉字符串左右两边的指定元素,默认是空格
strip 用法
newstr = string.strip(sub) > 括弧里需要传一个你想去掉的元素,可不填写
# strip()
str_name = "hello word!"
new_str = str_name.strip()
print(new_str)
# 打印结果
hello word!
# strip("h")
str_name = "hello word!"
new_str = str_name.strip('h')
print(new_str)
# 打印结果
ello word!
strip 扩展
传入的元素如果不在开头或结尾则无效
lstrip 仅去掉字符串开头的指定元素或空格
rstrip 仅去掉字符串结尾的指定元素或空格
相关
|