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


    # 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"))  # 指名道姓




  • 相关阅读:
    while 循环 。。
    数据运算,运算符
    字符串常用操作
    列表常用操作
    三级菜单
    杂七杂八
    简单的登陆程序001
    猜年龄游戏
    实现密文输入密码
    使用urllib2打开网页的三种方法
  • 原文地址:https://www.cnblogs.com/tingguoguoyo/p/10726598.html
Copyright © 2020-2023  润新知