python3 (2)字符串方法

注意的地方 IndentationError: unexpected indent 注意规范格式
1.join方法
在字符中间添加制定的符号或字符
test = "一二三四五"
v = ".".join(test)
print(v)

2.lower方法 upper方法和 islower方法 isupper方法
islower isupper 判断大小写,返回布尔值
lower upper 转换成大写或小写
test = "qwerdf"
v = test.upper()
q = test.islower()
p = test.isupper()
print(v,q,p)
test1 = "QWERT"
v1 = test1.lower()
q1 = test1.islower()
p1 = test1.isupper()
print(v1,q1,p1)

3.ljust和rjust方法 zfill方法
ljust(rjust) 在左(右)边添加字符,长度小于原字符串直接输出原字符串
zfill 返回指定长度的字符串,原字符串右对齐,前面填充0
test = "lili"
v = test.ljust(8,".")
v1 = test.rjust(8,"!")
v2 = test.zfill(10)
print(v,"\n",v1,"\n",v2)

4.strip方法
用于移除字符串头尾指定的字符(默认为空格)
test = "@@@我有一头小\t毛\n驴!!!!"
w = test.strip("@")
q = test.strip("!")
print(w,"\n",q)

5.split方法
通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串
test = "我 是 一, 种, 小小,小小猪"
# print(test.split())
# print(test.split(',',2))

6.python3字符串用len()方法来计算字符串的长度

猜你喜欢

转载自www.cnblogs.com/lq0310/p/8926981.html