• Python字符串—String


    1.什么是字符串(str)

    1)字符串是容器型数据类型(序列),以单引号和双引号作为容器标志,引号中的所有内容都是字符串的元素。
    2)字符串的元素字符串的元素又叫字符(注意:python中有字符的概念,但是没有字符类型长度是1的字符串可以看作是一个字符)
    a.普通字符:字母、数字、各国的文字和符号都能当作字符串(除特殊字符)
    b.转义字符:在字符串中一些一些特定的符号前加一个“+”来表示特殊的功能和意义
    ’ 代表 ’
    ‘’ 代表 ‘’
    \n 代表 换行
    \t 代表 tab键(制表符)
    \ 代表
    c.编码字符:\u4位16进制数 — 将4位16进制数对应的编码值转换为转义字符
    1)字符编码:计算机只有直接存储数字的能力,不能直接存储字符,所以在存储字符时都是存储了改字符对应的数字
    2)ASCII编码表和Unicode编码表
    ASCII编码表是美国国家标准根据美国的符号制定的,里面只包含部分特殊的字符、数字和字符(不包括日语和中文)
    python采用的是Unicode编码表,Unicode编码表是对ASCII表的扩展,包含了世界上所有国家的语言符号,又叫万国码
    Unicode编码表:中文范围:4e00~9fa5
    3)字符编码的相关方法
    chr(编码值) – 将编码值转换为字符
    ord(字符) – 将字符转换为编码值
    chr(97) – ‘a’
    ord(‘A’) – 65

    2.字符串操作

    ##1.获取字符串
    str1 = ‘hello world’
    print(str1[0]) – h
    print(str1[-1]) – d
    ##2.字符串切片
    str1 = ‘hello world’
    print(str1[2:7]) – ll0 w
    print(str1[2:7:2]) – l0w
    ##3.遍历
    for char in str1:
    print(char)
    ##4.+ ;*;==;!=;>;<;>=;<=
    字符串1+字符串2 -> 将字符串1和字符串2拼接在一起产生一个新的字符串
    字符串 * N / N * 字符串 -> 字符串重复N次产生一个新的字符串
    str1 = ‘abc’
    str2 = ‘123’
    print(str1 + str2) ---- ‘abc123’
    print(str1 * 2) ---- ‘abcabc’
    print(str1 == str2) — False
    print(str1 != str2) — True

    只能两个字符串比较大小(从前往后按顺序相比较编码值的大小)

    ‘0’ <= char <= ‘9’ 判断是否是数字字符
    ‘a’ <= char <= ‘z’ 判断是否是小写字母
    ‘A’ <= char <= ‘Z’ 判断是否是大写字母
    ‘\u4e00’ <= char <= ‘\u9fa5’ 判断是否是中文

    str1 = ‘abc’
    str2 = ‘123’
    print(‘abc’ > ‘bc’) # False a < b
    print(‘abcf’ > ‘abca’) # True f > a
    print(‘abdcdgh’ > ‘bcd’) # False

    ##in / not in
    ######字符串1 in 字符串2 — 判断字符串2中是否包含字符串1
    str1 = ‘abc’
    str2 = ‘abcde’
    str3 = ‘adbce’
    print(str1 in str2) # True
    print(str1 in str3) # False
    print(‘ab’ in str2) # False
    ##len, max, min, sorted, str
    字符串转换: 所有的数据都可以转换成字符串, 转换的时候是将数据放在引号中
    str3 = ‘how are you!’
    print(len(str3)) # 12

    注意: 转义字符串和编码字符的长度都是1

    str3 = ‘\how are\tyou!’
    print(len(str3)) # 13

    str3 = ‘\u4effhow are\tyou!’
    print(len(str3)) # 13

    str3 = ‘how are you!’
    print(max(str3)) # y
    print(sorted(str3)) # [’ ', ’ ', ‘!’, ‘a’, ‘e’, ‘h’, ‘o’, ‘o’, ‘r’, ‘u’, ‘w’, ‘y’]

    r语法

    ######在字符串的最前面加r或R,可以阻止字符串中所有的转义字符转义
    str1 = ‘\thow\nare’you!\u4e00’
    print(str1, len(str1))

    str1 = R’\thow\nare’you!\u4e00’
    print(str1, len(str1))

    格式字符串

    在字符串中用格式占位符表示字符串中不确定的部分
    a.语法: 包含格式占位符的字符 % (数据1, 数据2,…) - ()中数据的个数和类型要和前面格式占位符一一对应

    b.格式占位符
    %s - 字符串
    %d - 整数
    %.Nf - 浮点数,N控制小数点后小数的位数
    %c - 字符(可以将数字转换成字符)
    注意: 1)所有的数据都可以使用%s来做个数占位符 2)所有的数据都可以使用%s来接收

    1.对齐方式

    字符串.center(宽度, 填充字符=’ ‘) - 居中
    字符串.ljust(宽度, 填充字符=’ ‘) - 左对齐
    字符串.rjust(宽度, 填充字符=’ ')
    字符串.zfill(宽度) == 字符串.rjust(宽度, 0)
    str1 = ‘abc’
    print(str1.center(9, ‘+’)) # +++abc+++, 居中
    print(str1.center(6, ‘+’)) # +abc++, 居中
    print(str1.ljust(9, ‘+’)) # abc++++++, 左对齐
    print(str1.rjust(9, ‘+’)) # ++++++abc, 右对齐
    print(str1.zfill(9)) # 000000abc

    2.统计子串的个数

    字符串1.count(字符串2) - 统计字符串1中字符串2出现的次数

    str1 = ‘how are you! Im fine, thank you! and you?’
    print(str1.count(‘you’)) — 3
    print(str1.count(‘h’)) — 2
    print(str1.count(‘you’, 0, 12)) — 在下标是[0, 12)范围内统计’you’的个数
    ###3.获取子串下标
    print(str1.find(‘you’)) # 8
    print(str1.index(‘you’)) # 8
    print(str1.find(‘you1’)) # -1
    print(str1.index(‘you1’)) # ValueError: substring not found

    4.join方法

    字符串.join(序列) - 将序列中的元素用字符串连接产生一个新的字符串
    要求序列中的元素必须是字符串, 如果是字典key是字符串
    new_str1 = ‘+’.join(‘123’)
    print(new_str1) ‘1+2+3’
    new_str1 = ‘and’.join([‘小明’, ‘小花’, ‘小红’])
    print(new_str1) 小明and小花and小红

    5.替换

    字符串1.replace(字符串2, 字符串3) - 将字符串1中所有的字符串2都替换成字符串3
    字符串1.replace(字符串2, 字符串3, N) - 将字符串1中前N个字符串2替换成字符串3
    str1 = ‘how are you! Im fine, thank you! and you?’
    new_str1 = str1.replace(‘you’, ‘YOU’) – how are YOU! Im fine, thank YOU! and YOU?
    print(new_str1)
    new_str1 = str1.replace(‘you’, ‘me’, 2) – how are me! Im fine, thank me! and you?

    6.字符串切割

    字符串1.split(字符串2) - 将字符串2作为切割点切割字符串1, 返回一个列表
    str1 = ‘how are you! Im fine, thank you! and you?’
    str_list = str1.split(’ ')
    print(str_list) — [‘how’, ‘are’, ‘you!’, ‘Im’, ‘fine,’, ‘thank’, ‘you!’, ‘and’, ‘you?’]
    str_list = str1.split(‘I’)
    print(str_list) — ['how are you! ', ‘m fine, thank you! and you?’]

  • 相关阅读:
    java 问题解决
    报错:axios发送的所有请求都是404
    列举强制类型转换和隐式类型转换方式
    2022.7.31:航班延误
    docker及docker-compose的离线安装与镜像及容器的导入、导出操作总结
    【HCSD零代码云上开发】零代码入门微信小程序和物联网
    别再问我如何制作甘特图了!
    华为防火墙基础自学系列 | IKE介绍
    MPLS 初见
    Java Netty - Buffer类
  • 原文地址:https://blog.csdn.net/HuangXiongjin/article/details/127766338