• 6-字符串进阶


    字符串的长度和数量

    • print(len(s1))
    • print(s1.count('l'))
      • print(s1.count('l', 0, 5))

    大小写切换

    • title: 将每个单词的首字母大写,原来的非首字母大写会变成小写
    • capitalize :将整个字符串的首字母大写,原来的非首字母大写会变成小写
    • swapcase: 将大写变成小写,将小写变成大写

    提取

    • print(' aksad sfqs '.strip()) # aksad sfqs,默认将两边的空格去除
      print('-----韩--信-- --'.strip('-')) # 韩--信-- ,取出两边的'-'
      print('-----韩--信-- --'.lstrip('-')) # 韩--信-- --
      print('-----韩--信-- --'.rstrip('-')) # -----韩--信--

    eval()

    • 将字符串的表达式内容进行运算
    • 一次性输入多个数字并获取这些数字
    • a, b = eval(input('请输入2个数字(中间用逗号隔开):'))

    查找

    • find(): 从左往右查找字符串第一次出现的下标位置,如果不存在,则返回-1
    • rfind(): 从右往左查找字符串第一次出现的下标位置,如果不存在,则返回-1
    • print(s5.find('Py22')) # -1,判断子字符串是否在
    • index/rindex
      • print(s5.index('Py')) # 7
      • print(s5.rindex('Py')) # 18
      • print(s5.index('Py22')) # 报错,substring not found

    填充

    • center/ljust/rjust/zfill
      • # 填充(了解)
      • print('hello'.center(50))
      • print('hello'.center(50, '-')) # ----------------------hello-----------------------
      • print('hello'.ljust(50, '-')) # hello---------------------------------------------
      • print('hello'.rjust(50, '-')) # ---------------------------------------------hello
      • print('hello'.zfill(50)) # 000000000000000000000000000000000000000000000hello

    判断

    • print('HE'.isupper()) # 是否为大写字母
    • print('h'.islower()) # 是否为小写字母
    • print('323'.isdigit()) # 是否为数字
    • print('a'.isalpha()) # 是否为字母
    • print('abc1223'.isalnum()) # 是否为数字或字母
    • print('Hello World'.istitle()) # 是否每个单词首字母大写,其他小写

    分割

    • s1 = 'baoqiang is a green man'
      • print(s1.split()) # ['baoqiang', 'is', 'a', 'green', 'man']
      • print(s1.split(' ')) # ['baoqiang', '', 'is', 'a', 'green', 'man']
      • print(s1.split('a')) # ['b', 'oqi', 'ng is ', ' green m', 'n']
      • print(s1.split('baoqiang')) # ['', ' is a green man']
      • print(s1.split('marong')) # ['baoqiang is a green man'],不存在则得到整个字符串组成的列表
      • print(s1.split('a', 2)) # ['b', 'oqi', 'ng is a green man']
    • # 按行分割
      • print(s2.splitlines()) # ['床前明月光', '疑是地上霜', '举头望明月', '低头思故乡']
      • print(s2.splitlines(True)) # ['床前明月光 ', '疑是地上霜 ', '举头望明月 ', '低头思故乡']
      • print(s2.split(' ')) # ['床前明月光', '疑是地上霜', '举头望明月', '低头思故乡']

    合并

    • 用分割符进行合并
    • join只能拼接字符串
      • print(''.join(l1)) # 床前明月光疑是地上霜举头望明月低头思故乡
      • print(' '.join(l1))

    替换

    • s4 = s3.replace('nice', 'good', 2) # 贾乃亮 is a good good nice man,只替换前两个

    简单加密

    • t = str.maketrans('aco', '123')
    • print(t) # {97: 49, 99: 50, 111: 51}
    • print('today is a good day'.translate(t)) # t3d1y is 1 g33d d1y

    以指定字符开头结尾

    • print('hello world'.startswith('hello')) # True,是否以指定字符串开头
    • print('hello world'.endswith('world')) # True,是否以指定字符串结尾

    编码/解码

    • 编码
      • 将字符串转换为二进制(默认是utf-8)
        • r = s5.encode()
      • ord(): 将字符转换成ASCII
        • print(ord('a'))
    • 解码
      • 将二进制转换为字符串
        • print(r.decode())
      • chr(): 将ASCII转换成字符
        • print(chr(23))
  • 相关阅读:
    Java中替换字符串中特定字符,replaceAll,replace,replaceFirst的区别
    牛客剑指offer 67题(持续更新~)
    从尾到头打印链表
    字符串变形
    缩写
    删除公共字符
    替换空格
    二维数组的查找
    acm博弈论基础总结
    acm模板总结
  • 原文地址:https://www.cnblogs.com/lotuslaw/p/14006529.html
Copyright © 2020-2023  润新知