zoukankan      html  css  js  c++  java
  • 字符串的魔法

    大小写转换

    test = "alex"
    v = test.capitalize()
    print(v)  # Alex,首字母大写
    # lower() 方法只对ASCII编码,也就是‘A-Z’有效,对于其他语言(非汉语或英文)中把大写转换为小写的情况只能用 casefold() 方法。
    v1 = test.casefold()
    print(v1)  # alex 所有的变小写,这个更牛逼,除英文字母小写转换外,还有其他特殊字符的大小写转换. eg:# 德语的"ß"正确的小写是"ss"
    v2 = test.lower()
    print(v2)  # alex 所有的变小写
    
    # 是否是小写以及转换为小写
    test = "Alex"
    print(test.islower())
    print(test.lower())
    # 是否是大写以及转换为大写
    test = "Alex"
    print(test.isupper())
    print(test.upper())
    
    # 大小写转换
    test = "aLex"
    print(test.swapcase())

     填充

    test = "alex"
    print(test.center(20, '*'))  # ********alex********
    # 设置宽度,并将内容居中.20代表总长度,*代表空白位置填充.
    print(test.ljust(20, '*'))  # alex****************
    print(test.rjust(20, '*'))  # ****************alex
    print(test.zfill(20))  # 这个不能指定字符,默认只用0填充

    endswith 和 startswith

    test = "alex"
    print(test.endswith("a"))  # 表示以什么结尾. False
    print(test.endswith("x"))  # True
    print(test.startswith("a"))  # True
    print(test.startswith("e"))  # 表示以什么开始,False

     查找

    test = "alexalex"
    v1 = test.count("ex")  # 字符串中,寻找子序列出现的次数.
    print(v1)
    v1 = test.count("ex", 5)  # 5 表示从第几个开始往后找
    print(v1)
    v1 = test.count("ex", 5, 6)  # 表示从5-6的范围中找
    print(v1)
    
    test = "alexalex"
    print(test.find("ex"))  # 2, 从开始往后找,找到第一个获取其位置.
    print(test.find("ex", 5, 7))  # -1, 若返回-1 ,表示没找到
    print(test.find("ex", 5, 8))  # 6, 能找到,注意开区间闭区间. 5<= <8
    
    # print(test.index("ex")) # index与find的功能类似.
    # 区别: index找不到,报错.find 找不到返回-1

    格式化

    # 格式化,将字符串中的占位符替换为指定的值
    test = "i am {name}, age is {age}"
    print(test.format(name="cql", age=19))
    # 根据顺序替换
    test = "i am {0}, age is {1}"
    print(test.format("cql", 19))
    test = "i am {name}, age is {age}"
    test.format_map({"name": "alex", "age": 19})

    替换

    # 替换
    test = "asdfsjfklsdasjlfkjsd"
    print(test.replace("as", "ha"))
    print(test.replace("as", "ha", 1))  # 只替换一个


    # maketrans 和translate 一起搭配使用
    m = str.maketrans("sdfjdsfs", "12345678")
    test = "lksdjfwoeiurioefljsldfjlksdjflkds"
    new_test = test.translate(m)
    print(new_test) # lk8547woeiurioe7l48l574lk8547lk58

    内容判断

    test = "user899_+"
    print(test.isalnum())  # False, 判断字符串中是否只是包含字母和数字.
    test = "hahah"
    print(test.isalpha())  # True, 字母
    test = "123"
    print(test.isdigit())  # True, 判断输入的值是否是数字,只能判断正常的数字
    print(test.isdecimal()) # True
    test = ""
    print(test.isdigit())  # True判断输入的值是否是数字,支持特殊符号.
    print(test.isdecimal()) # False
    test = ""
    print(test.isnumeric()) # True
    test = ""
    print(test.isnumeric())  # True 判断的数字,支持正常的数字特殊符号以及中文
    # 用的比较多的是isdecimal
    
    test = ""
    print(test.isprintable())
    test = "   "
    print(test.isspace())  # 判断是不是全部都是空格
    
    test = "I am a student"
    print(test.istitle())
    print(test.title())  # 转换为标题
    print(test.istitle())  # 首字母大写判断为标题

     关键字判断

    # 字母数字下划线: 标识符. def class 符合前面的规则.
    a = "_123"
    import keyword
    
    print(keyword.iskeyword("def"))
    print(a.isidentifier())

    字符串分隔

    test = "你是风儿我是沙"
    print(test)
    t = '_'
    print(t.join(test))  # 字符串根据指定分隔符进行拼接
    
    # 正则表达式,可以设置是否要显示分隔的元素.
    # partition 只能分三份,split可以指定
    test = "testsldufllsdkjf"
    print(test.partition("s"))  # 找到第一个s作为分隔,分隔成三分('te', 's', 'tsldufllsdkjf')
    print(test.rpartition("s"))  # ('testsldufll', 's', 'dkjf')
    print(test.split("s", 2))  # 根据s作为分隔, ['te', 't', 'ldufllsdkjf']
    print(test.rsplit("s", 2))  # ['test', 'ldufll', 'dkjf']
    
    # 只能根据换行分割. true false是否保留换行
    test = "slkdjflkdsjflkdjslkf
    sldkfjlds
    sldkfjlsd
    "
    print(test.splitlines(True))
    print(test.splitlines(False))
    print(test.splitlines())

    去除

    # 去除左右空白
    test = " alex "
    print(test.lstrip())
    print(test.rstrip())
    print(test.strip())
    
    # 还可以去除
     p 	
    test = "
    alex "
    print(test.lstrip())
    print(test.rstrip())
    print(test.strip())
    
    # 还可以去除指定字符
    test = "alex"
    print(test.lstrip("al"))
    print(test.rstrip("x"))
    print(test.strip())

     制表格

    s = "12345678	9"
    print(s.expandtabs(6))
    # 每6个进行判断,若包含	,不足的补空格总计6个宽度.可以用来制作表格.
    #断句, 根据数字进行断句作为一组, 找到	, 前面的加空格, 总共6个.

    总结:

    字符串的7个基本魔法:

    join
    split
    find
    strip
    upper
    lower
    replace

    5个灰魔法(除字符串类型,其他类型也有的)

    # 索引下标获取字符串的某一个字符
    test = "alex"
    print(test[0])
    print(test[3])
    
    # 切片
    print(test[0:2])  # 范围0 <= <2
    print(test[0:-1])  # 到最后的位置
    
    # len 有多少个字符串
    print(len(test))
    print(len("中华人民共和国"))
    
    #note:python3获取当前字符串有几个字符组成
    #python2 utf-8.一个汉字3个字节,就会拿到3 * 7
    
    # while 和for 循环遍历字符串
    index = 0
    while index < len(test):
        print(test[index])
        index += 1
    
    for item in test:
        print(item)
    
    test = "中国人民解放军"
    for item in test:
        print(item)
        break
    
    for item in test:
        continue
        print(item)
    
    # range,创建连续的数字
    print(range(100))
    print(range(0, 100))
    # 还能创建非连续的.设置步长
    print(range(0, 100, 5))
    # python2 会打印所有
    # python3 不会打印所有,内存优化机制.
    for item in range(100):
        print(item)
    # 在其他的数据类型也能用

    字符串的深魔法

    # 字符串一旦创建,不可修改
    # 一旦修改或者拼接,就会产生一个新的字符串
    name = "cql"  # 在内存里创建一个cal
    age = 18  # 在内存里创建一个18
    info = name + str(age)  # 在内存里创建一个cql18
    print(info)
  • 相关阅读:
    字符编码
    IO流技术
    TreeMap使用和Comparable比较
    Collections工具类
    使用迭代器进行遍历时
    238. 除自身以外数组的乘积
    python 字典按键、值排序
    collections.Counter用法
    442. 数组中重复的数据
    1395. 统计作战单位数
  • 原文地址:https://www.cnblogs.com/Windows-phone/p/9687328.html
Copyright © 2011-2022 走看看