首页 > 编程语言 > 详细

PythonStudy——字符串重要方法 String important method

时间:2019-04-17 22:52:40      阅读:109      评论:0      收藏:0      [点我收藏+]
# 1.索引(目标字符串的索引位置)
s1 = 123abc呵呵
print(s1.index(b))
# 2.去留白(默认去两端留白,也可以去指定字符)
s2 = ***好 * 的  ***
print(s2.strip(*))
# 3.计算子字符串个数
s3 = 12312312
print(s3.count(123))
# 4.判断字符串是否是数字:只能判断正整数
s4 = 123
print(s4.isdigit())
# 5.大小写转换
s5 = "AbC def"
print(s5.upper())  # 全大写
print(s5.lower())  # 全小写
# 了了解
print(s5.capitalize())  # 首字母大写
print(s5.title())  # 每个单词首字母大写
# 6.以某某开头或结尾
s6 = https://www.baidu.com
r1 = s6.startswith(https:)
r2 = s6.startswith(http:)
r3 = s6.endswith(com)
r4 = s6.endswith(cn)
if (r1 or r2) and (r3 or r4):
    print(合法的链接)
else:
    print(不合法的链接)
# 7.替换
s7 = egon say: he is da shuai b,egon!egon!egon!
new_s7 = s7.replace(egon, Liu某)  # 默认替换所有
print(new_s7)
new_s7 = s7.replace(egon, Liu某, 1)  # 替换一次
print(new_s7)
# 8.格式化
s8 = name:{},age:{}
print(s8.format(Owen, 18))  # 默认按位置
print(name:{1},age:{1}, height:{1}.format(Owen, 18))  # 标注位置,一个值可以多次利用
print(name:{n},age:{a}, height:{a}.format(a=18, n="Zero"))  # 指名道姓

 





PythonStudy——字符串重要方法 String important method

原文:https://www.cnblogs.com/tingguoguoyo/p/10726598.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!