• Python开发——数据类型【字符串】


    字符串定义

      字符串是一个有序的字符的集合,用于存储和表示基本的文本信息

      在Python中加了引号的字符,都被认为是字符串!

      单引号、双引号、多引号之间的区别? 答案:单双引号没有区别

      多引号的作用? 答案:多行字符串必须用引号

    字符串的特性

      字符串是按照从左向右的顺序定义字符集和下标从0开始顺序访问有序

      补充:

        字符串的单引号和双引号都无法取消特殊字符的含义,

          如果想让引号内所有字符均取消特殊意义,在引号前面加r,如 name = r'l hf'。

        unicode字符串与r连用必须在r前面,如 name = ur'l hf'。

      字符串一旦创建,不可修改当字符串修改或者拼接,将会重新生成字符串

    字符串常用操作

    字符串拼接

    1 # 字符串的拼接,只能双方都是字符串
    2 name= 'Lucy '
    3 age= '18'
    4 print(name+age) # Lucy 18
    5 print(name*10) # 复制自身10次,再拼接在一起
    6 # Lucy Lucy Lucy Lucy Lucy Lucy Lucy Lucy Lucy Lucy

    索引

    1 s = 'hello'
    2 print(s[1])         # e
    3 print(s[-1])        # o
    4 print(s.index('e')) # 1

    查找

    1 s = 'hello'
    2 print(s.find('o'))  # 4
    3 print(s.find('i'))  # -1

    长度

    1 s = 'hello world'
    2 print(len(s))   # 11

    切片

     1 s = 'abcdefghijklmn'    # 左闭[、右开)、步长
     2 print(s[0:7])       # abcdefg
     3 print(s[:7])        # abcdefg
     4 print(s[0:7:2])     # aceg
     5 print(s[:7:2])      # aceg
     6 
     7 print(s[7:14])      # hijklmn
     8 print(s[7:])        # hijklmn
     9 print(s[7:14:3])    # hkn
    10 print(s[7::3])      # hkn
    11 
    12 print(s[:])         # abcdefghijklmn
    13 print(s[::2])       # acegikm
    14 print(s[::-1])      # nmlkjihgfedcba

    字符串函数

    In 的使用

    1 s = "helloworld"
    2 if "l" in s:
    3     print("ok")
    4 
    5 for items in "hello":
    6     print(items)

     字符串拼接join

    1 # 将字符串中的每一个元素按照指定分隔符进行拼接
    2 s = "PYTHON"
    3 v = "_".join(s)
    4 print(v)        # P_Y_T_H_O_N

    字符串大小写转换

     1 # capitalize 首字母大写
     2 s = 'hello world'
     3 v = s.capitalize()
     4 print(v)                # Hello world
     5 # casefold 大写转小写
     6 s = 'Hello WORLD'
     7 v = s.casefold()
     8 print(v)                # hello world
     9 # lower 大写转小写
    10 v = s.lower()
    11 print(v)                # hello world
    12 # 判断是否全部是大(小)写 和 转换为小(大)写
    13 s = "Hello World"
    14 v = s.islower()
    15 print(v)            # False
    16 v = s.lower()
    17 print(v)            # hello world
    18 
    19 v = s.isupper()
    20 print(v)            # False
    21 v = s.upper()
    22 print(v)            # HELLO WORLD
    23 # 大写转小写,小写转大写
    24 v = s.swapcase()
    25 print(v)            # hELLO wORLD

    字符串分割

     1 # 分割为三部分
     2 s = "testasdsddfg"
     3 v = s.partition("s")    # 以字符出现的第一个位置分割
     4 print(v)                # ('te', 's', 'tasdsddfg')
     5 v = s.rpartition("s")   # 以字符出现的最后一个位置分割
     6 print(v)                # ('testasd', 's', 'ddfg')
     7 # 分割为指定数量处
     8 v = s.split("s",2)      # 以字符出现的前两个位置处分割
     9 print(v)                # ['te', 'ta', 'dsddfg']
    10 v = s.rsplit("s",2)     # 以字符出现的最后两个位置处分割
    11 print(v)                # ['testa', 'd', 'ddfg']
    12 # 换行分割
    13 s = "asdfadfasdf
    asdfasdf
    adfasdf"
    14 v = s.splitlines()  # 默认为False
    15 print(v)            # ['asdfadfasdf', 'asdfasdf', 'adfasdf']
    16 v = s.splitlines(True)
    17 print(v)            # ['asdfadfasdf
    ', 'asdfasdf
    ', 'adfasdf']

    字符串移除

     1 # 移除指定字符串
     2 # 有限最多匹配
     3 s = "Alex"
     4 v1 = s.strip("ex")      # Al
     5 v2 = s.lstrip("Al")     # ex
     6 v3 = s.rstrip("ex")     # Al
     7 print(v1)
     8 print(v2)
     9 print(v3)
    10 print("*************")
    11 # 去除左右空白
    12 s = " hello world "
    13 v1 = s.strip()          # hello world
    14 v2 = s.lstrip()         # hello world
    15 v3 = s.rstrip()         #  hello world
    16 print(v1)
    17 print(v2)
    18 print(v3)
    19 print("*************")
    20 # 去除	 
    
    21 s = "	hello world"
    22 v1 = s.strip()
    23 v2 = s.lstrip()
    24 v3 = s.rstrip()
    25 print(s)                #     hello world
    26 print(v1)               # hello world
    27 print(v2)               # hello world
    28 print(v3)               #   hello world

    字符串格式化

    1 # format格式化
    2 s = 'I am {name},I am {age}'
    3 v = s.format(name = 'yuan',age = '18')
    4 print(v)                # I am yuan,I am 18
    5 v = s.format_map({'name':'yuan','age':'18'})
    6 print(v)                # I am yuan,I am 18
    7 s = 'I am {0},I am {1}'
    8 v = s.format('yuan','18')
    9 print(v)                # I am yuan,I am 18        

    字符串替换

    1 # 将指定字符串替换为指定字符串
    2 s = "Hello World"
    3 v = s.replace("o","k")
    4 print(v)                    # Hellk Wkrld
    5 v = s.replace("o","k",1)    # (旧字符串、新字符串、数量)
    6 print(v)                    # Hellk World

    字符串子序列查找

    1 # count 在字符串中寻找,查找子序列的出现次数
    2 s = "aLexalexr"
    3 v = s.count("ex")
    4 print(v)        # 2
    5 v = s.count("ex",5,6)       # (子序列,起始位置,结束位置)
    6 print(v)        # 0

    tab填充

    1 # expandtabs 设置字符串长度,其余用tab键,	 填充
    2 s = "username	email	password
    laiying	ying@q.com	123
    laiying	ying@q.com	123
    laiying	ying@q.com	123"
    3 v = s.expandtabs(20)
    4 print(v)
    5 # username            email               password
    6 # laiying             ying@q.com          123
    7 # laiying             ying@q.com          123
    8 # laiying             ying@q.com          123

    以···开头(结尾)

    1 # startswith 以。。。开始
    2 # endswith 以。。。结尾
    3 s = "Hello World"
    4 v = s.endswith("ld")
    5 print(v)    # True
    6 v = s.startswith('He')
    7 print(v)    # True

    判断字符串

     1 # 判断字符串 是否为标题
     2 s = "I am a girl"
     3 v = s.istitle()
     4 print(v)        # False
     5 v = s.title()
     6 print(v)        # I Am A Girl
     7 v = v.istitle()
     8 print(v)        # True
     9 
    10 # 判断 字符串中是否只包含 字母和数字
    11 s = '123ada'
    12 v = s.isalnum()
    13 print(v)        # True
    14 # 判断 字符串中是否只包含 字母和汉字
    15 s = 'asas我是'
    16 v = s.isalpha()
    17 print(v)        # True
    18 
    19 # 判断是否为数字
    20 s = ""
    21 v1 = s.isdecimal()
    22 v2 = s.isdigit()
    23 v3 = s.isnumeric()
    24 print(v1,v2,v3)
    25 # s = "二"    False False True
    26 # s = "1"      True True True
    27 # s = "②"      False True True
    28 
    29 # 判断字符串 是否存在不可显示的字符
    30 # 如:
    、	
    31 s = 'asdf	asd'
    32 v = s.isprintable()
    33 print(v)        # False
    34 
    35 # 判断字符串 是否为空格
    36 s = " "         # 若"",则False
    37 v = s.isspace()
    38 print(v)        # True

    字符串设置宽度及填充

     1 # center 设置宽度,并将内容居中
     2 s = 'Hello World'
     3 v = s.center(20)        # 20 指字符串总长度
     4 print(v)                #     Hello World
     5 v = s.center(20,"*")    # * 为空白填充【可无】
     6 print(v)                # ****Hello World*****
     7 v = s.ljust(20,"*")
     8 print(v)                # Hello World*********
     9 v = s.rjust(20,"*")
    10 print(v)                # *********Hello World
    11 v = s.zfill(20)
    12 print(v)                # 000000000Hello World

    对应关系替换maketrans、translate

     isidentifier【】

    1 # identifier:识别符; 检验人,标识符; 鉴别器; 编号
    2 # 字母,数字,下划线 : 标识符 def  class
    3 s = "asdfghjk"
    4 v = s.isidentifier()
    5 print(v)            # True
    6 # s = "123"     False
    7 # s = "class"   True    
  • 相关阅读:
    Kubernetes List-Watch
    Go 模板语法
    vRA7 Business error “Untrusted certificate chain”
    Centos 7/8 安装 Harbor
    Kubernetes Headless Service
    Kubernetes addon-manager
    Kubernetes Metrics-Server
    Kubernetes Heapster
    容器rootfs
    Kubernetes lxcfs
  • 原文地址:https://www.cnblogs.com/yuanlili/p/8560391.html
Copyright © 2020-2023  润新知