zoukankan      html  css  js  c++  java
  • Python3-笔记-B-005-数据结构-字符串str

    # 字符串[有序不可变Unicode序列]
    def strs():
    # === 创建字符串 ===
    strs = "Hello World!" # 字符串用 '/ " / ''' / """ 包裹
    strs = """可多行的字符串"""
    strs = str(123)
    strs = str({1, 2, 3}) # 将对象转为字符串, '{1, 2, 3}'
    strs = str(b'xe4xbdxa0xe5xa5xbd', "utf8") # bytes 转为字符串, '你好'
    strs = r"原始字符串" # 原始字符串,'原始字符串'
    strs = u"Unicode字符串" # Unicode字符串,'Unicode字符串'

    # 截取字符串
    strs = "这是汉字测试Hello World!"
    s1 = strs[4] # 截取一个,''
    s2 = strs[6:11] # 截取 6-11 (0开始), 'Hello'
    s3 = strs[6:] # 截取 6-末尾, 'Hello World!'
    s4 = strs[-6:] # 截取 倒数6-末尾 (倒数1开始), 'World!'
    s5 = strs.split("l") # 根据 关键词 分割字符串(返回列表), <class 'list'>: ['这是汉字测试He', '', 'o Wor', 'd!']
    s6 = strs.split("l", 2) # 2为限制分割次数, <class 'list'>: ['这是汉字测试He', '', 'o World!']
    s7 = strs.partition("l") # 分割成三部分(/l/), <class 'tuple'>: ('这是汉字测试He', 'l', 'lo World!')
    s8 = strs.rpartition("l") # <class 'tuple'>: ('这是汉字测试Hello Wor', 'l', 'd!')
    s9 = "Hello World!".splitlines() # 按行分割, <class 'list'>: ['Hello', 'World!']

    # 查找
    index = strs.find("lo") # 查找 lo 字符串(首次)的索引, 未找到 -1, =>9
    index = strs.find("H", 0, 18) # 参数2end位置, =>5
    index = strs.rfind("l") # 从右开始查找,但索引是从左边开始算得,=>15
    index = strs.rfind("l", 0, 10)
    index = strs.index("lo") # 查找 lo 字符串(首次)的索引, 未找到 抛异常
    index = strs.index("lo", 0, 20)
    index = strs.rindex("l") # 从右开始查找
    index = strs.rindex("l", 0, 10)
    index = strs.startswith("Hello") # 是否以该字符串开头
    index = strs.endswith("!")
    m1 = min(strs) # ' '
    m2 = max(strs) # ''

    # 替换
    s1 = strs.replace("l", "o") # '这是汉字测试Heooo Worod!'
    s2 = strs.replace("l", "o", 2) # 2为限制替换次数, '这是汉字测试Heooo World!'

    # 遍历
    for i in strs:
    print("for:%s"%i)

    # 统计
    length = len(strs) # 字符串长度, 18
    count = strs.count("l") # 统计 l 字符串出现的次数, 3
    count = strs.count("l", 0, 10) # 2

    # 转换
    strs = "hELLOo这是汉字测试 World!"
    s1 = strs.capitalize() # 将首字符大写,其余全小写, 'Helloo这是汉字测试 world!'
    s2 = strs.lower() # 所有字符转为小写, 'helloo这是汉字测试 world!'
    s3 = strs.casefold() # (lower)
    s4 = strs.upper() # 'HELLOO这是汉字测试 WORLD!'
    s5 = strs.swapcase() # 大小写字母取反, 'HelloO这是汉字测试 wORLD!'
    s6 = strs.encode(encoding='utf-8', errors='strict') # 编码成("utf-8")bytes(同通过bytes.decode()解码)
    # b'hELLOoxe8xbfx99xe6x98xafxe6xb1x89xe5xadx97xe6xb5x8bxe8xafx95 World!'

    # 格式化
    s1 = strs.ljust(30) # 左对齐, 20为补充长度(字符数不足20则自动补充空格至20), 'hELLOo这是汉字测试 World! '
    s2 = strs.rjust(30) # ' hELLOo这是汉字测试 World!'
    s3 = strs.center(30) # ' hELLOo这是汉字测试 World! '
    s4 = strs.zfill(30) # 左边填充0到指定宽度, '00000000000hELLOo这是汉字测试 World!'
    s5 = s3.lstrip() # 删除左边空格. 'hELLOo这是汉字测试 World! '
    s6 = s3.rstrip() # ' hELLOo这是汉字测试 World!'
    s7 = strs.title() # 标题 (每个单词首字母大写). 'Helloo这是汉字测试 World!'
    s8 = "-".join(strs) # 每个相邻字符中间插入关键词(注意:关键词调用该函数), 'h-E-L-L-O-o------- -W-o-r-l-d-!'
    strs.expandtabs() # " "8位空格代替(注意:类似tab键的空格补充,而非直接将 转为8位空格补充)
    s9 = '{name},{age}'.format(age=21,name='柳岩') # 自动格式化 ,'柳岩,21'
    s0 = "6a6b6c6".translate(str.maketrans("abc", "123")) # 根据映射表格式化, '6162636'

    # 判断
    boolean = "-123".isdigit() # 是否是(正整)数字(注意:是的关系,非包含的关系) f
    boolean = '123'.isnumeric() # (isdigit) t
    boolean = 'abc汉字'.isalpha() # 是否是字母 t
    boolean = 'abc123汉字'.isalnum() # 是否是字母和数字(可混合) t
    boolean = ' '.isspace() # 是否是空格 t
    boolean = 'A汉字123B'.isupper() # 是否是大写字母(可含数字) t
    boolean = 'a汉字321b'.islower() # t
    boolean = "-321".isdecimal() # 是否是十进制数,貌似得是正整数 f
    boolean = '1_a'.isidentifier() # 是否是合法标识符 f
    boolean = 'ab123&*'.isprintable() # 是否可打印(含转义符=false) tboolean = 'Aaa Bb A123'.istitle() # 是否是标题 (单词首字母大写,空格分隔的视为标题),还不能有汉字。。。
     
  • 相关阅读:
    Mysql之存储过程与存储函数
    mysql-bin日志自动清理及手动删除
    mysql下面的binlog
    mysql下的数据备份与恢复
    查询mysql数据库中各个表所占空间大小以及索引大小
    mysql执行sql语句报错this is incompatible with sql_mode=only_full_group_by
    docker WARNING: IPv4 forwarding is disabled. 解决方法
    Linux平台修改环境变量的方式
    PuTsangTo
    (一) 从Angular1到Angular2的杂谈
  • 原文地址:https://www.cnblogs.com/vito13/p/7729975.html
Copyright © 2011-2022 走看看