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

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

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

# 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")) #赋值

print("   good morning\n".strip()) #前后的换行符  空格都会被去掉 分L R
print("OK")
print("good morning".replace("good","nice"))# 输入内容更换
print("good morning".split(" "))#以什么内容 分割 变成列表 用内容去分割 内容消失当成分隔符使用
print("good morning".title())# 文本变成标题形式 首字母大写

#重要
print(st .count("l"))# 3 数l的个数
print("good morning".title())# 文本变成标题形式 首字母大写
print(st .startswith("ld"))  #False 以什么开头 错误Falce 对的就是True
print(st .find("l"))
print("   good morning\n".strip()) #前后的换行符  空格都会被去掉 分L R
print("good morning".split(" "))#以什么内容 分割 变成列表 用内容去分割 内容消失当成分隔符使用

猜你喜欢

转载自www.cnblogs.com/hanhankeji/p/12010114.html