学习python的第十七天【字符串内置】

限于喜欢 提交于 2019-12-07 15:09:41

字符串:

#__author:"hanhankeji"
#date: 2019/12/7

# string 字符串
a = "hello"
print(a) #hello
#可以计算
print(a*2) #hellohello
# 切片去取内容
print(a[2::]) #llo
#in 关键字去判断是不是在容器里面
print("ll" in a) #True 如果内容在里面返回正确值
#格式化 %表示
#字符串的拼接
aa = "123"
bb = "456"
cc = aa + bb
print(cc)#123456
print( "".join([aa,bb]))#123456 用join 拼接

#字符串的内置方法:
st = "hello w\torld{name}"
print(st .count("l"))# 3 数l的个数
print(st .capitalize())#首字母大写 Hello world
print(st .center(50,"*")) #*******************hello world********************居中文本在符号中间
print(st .endswith("ld")) #True 以XX结尾 不对就Falce
print(st .startswith("ld"))  #False 以什么开头 错误Falce 对的就是True
print(st .expandtabs(tabsize=5))  #hello w   orld  \t 的空格数量、
print(st .find("o"))#4 查找指定内容出现第一次位置,展示索引值的值01234
print(st .format(name= "hanhankeji")) #赋值

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!