吕布:访问字符串中的值
Python访问子字符串,可以使用方括号来截取字符串,如下实例:
v1="Hello World!" v2="Python Runoob" print("v1[0]:",v1[0]) print("v2[1:3]:",v2[1:3])
1、首字母大写
text="hello" v=text.capitalize() print(v)
2、所有字母小写
test1="heLLo" v1=test.lower() print(v1)
3、设置宽度,并将内容居中
text="hello" v=text.center(20,"好")#20代指总长度;*空白未知填充,一个字符 print(v)
4、计算字符出现的次数
test="hellohello" print(test.count("l"))#4 print(test.count("h"))#2
5、是否以指定字符结尾/开始?
test="hello" v=test.endswith("o") print(v)#True v2=test.startswith("h") print(v2)#True v3=test.endswith("l") print(v3)#False