zoukankan      html  css  js  c++  java
  • 028 字符串类型内置方法

    一、字符串类型内置方法(str)

    1. 用途:

    描述性质的东西

    2. 定义方式:

    使用' '、" "、''' '''、""" """包裹的的一串字符

    • u'unicode': unicode编码的字符串
    • b'101': 二进制编码的字符串
    • r' ': 原生字符串,也就是说' '这是普通的两个字符,并没有换行的意思
    name = 'xucheng'  # name =str('xucheng')
    s1 = str(1.1)
    s2 = str([1, 2, 3])
    
    print(f's1:{s1}, type:{type(s1)}')
    print(f's2:{s2}, type:{type(s2)}')
    

    s1:1.1, type:<class 'str'>
    s2:[1, 2, 3], type:<class 'str'>

    3. 内置方法:

    常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)、其他操作(了解)三个部分。

    3.1 优先掌握 (必须得掌握)

    1. 按索引取值
    2. 切片
    3. 长度len
    4. 成员运算in|not in
    5. 移除空白strip
    6. 切分split
    7. 循环

    1.按索引取值(只可取不可改变)

    # 1. 索引取值
    s = "abcdefg"
    print(f"{'索引取值':-^100}")
    print(s[1])
    

    ------------------------------------------------索引取值------------------------------------------------
    d

    2.切片(顾头不顾尾,步长)

    print(f"{'切片':-^100}")
    s = "abcdefg"
    print(s[0:4:1])  # 1表示从左到右
    print(s[-4::-1])   # -1表示从右到左  # 不推荐掌握
    print(s[4:0:-1])   # -1表示从右到左
    

    -------------------------------------------------切片-------------------------------------------------
    abcd
    dcba
    edcb

    3.长度len

    print(f"{'长度len':-^100}")
    s2 = 'xucheng handsome'
    print(len(s2))  # 求字符串的长度
    

    -----------------------------------------------长度len------------------------------------------------
    16

    4.成员运算in|not in

    print(f"{'in 或 not in':-^100}")
    s = 'xucheng handsome'
    print('*' in s2)  # True
    print('$' not in s2)  # True
    

    --------------------------------------------in 或 not in---------------------------------------------
    True
    True

    5.移除空白strip

    print(f"{'split':-^100}")
    s2 = '***!!!!!xucheng handsome----***'
    print(s2.split())  # 默认以空格切割字符串
    print(s2.split('!'))  # 以!切割
    print(s2.split('!',2))
    

    -----------------------------------------------strip------------------------------------------------
    xucheng handsome
    xucheng handsome
    !!!!!xucheng handsome----
    xucheng handsome

    6.切分split

    print(f"{'split':-^100}")
    s2 = '***!!!!!xucheng handsome----***'
    print(s2.split())  # 默认以空格切割字符串
    print(s2.split('!'))  # 以!切割
    print(s2.split('!',2))
    

    -----------------------------------------------split------------------------------------------------
    ['!!!!!xucheng', 'handsome----']
    ['', '', '', '', '', 'xucheng handsome----']
    ['********', '', '!!!xucheng handsome----']

    7.循环

    print(f"{'for循环':-^100}")
    s = 'xucheng handsome'
    for i in s:
        print(i)
    

    -----------------------------------------------for循环------------------------------------------------
    x u c h e n g h a n d s o m e

    3.2 需要掌握

    1. lstrip&rstrip
    2. lower&upper
    3. startswith&endswith
    4. rsplit
    5. join
    6. replace
    7. isdigit(纯数字)/isalpha(纯字母)

    1.lstrip() 和 rstrip()

    print(f"{'lstrip() 和 rstrip()':-^100}")
    s2 = '***!!!!!nick handsome----***'
    print(s2.lstrip('*'))       # 从字符串中去掉左边的字符"*"
    print(s2.rstrip('*'))       # 从字符串中去掉右边的字符"*"
    
    

    ----------------------------------------lstrip() 和 rstrip()-----------------------------------------
    !!!!!xucheng handsome----*******
    ***!!!!!xucheng handsome----

    2.lower&upper

    print(f"{'lower&upper':-^100}")
    s3 = 'aaabbJ'
    print(s3.lower())          
    print(s3.upper())           # AAABBJ
    
    

    --------------------------------------------lower&upper---------------------------------------------
    aaabbj
    AAABBJ

    3.startswith&endswith

    print(f"{'startswith&endswith':-^100}")
    s3 = 'aaabbJ'
    print(s3.startswith('b'))   # False
    print(s3.endswith('J'))     # True
    
    

    ----------------------------------------startswith&endswith-----------------------------------------
    False
    True

    4.rsplit()

    print(f"{'rsplit()':-^100}")
    s2 = '***!!!!!xucheng handsome----***'
    print(s2.split('*', 1))     # split默认从左切割,只切割掉一个字符
    print(s2.rsplit('*', 1))    # rsplit从右切割,只切割掉一个字符
    
    

    ----------------------------------------------rsplit()----------------------------------------------
    ['', '**!!!!!xucheng handsome----***']
    ['***!!!!!xucheng handsome----**', '']

    5.join(用的比较多)一般和split联用

    print(f"{'join(用的比较多)一般和split联用':-^100}")
    s3 = ' '
    print(s3.join(['234', '234', '234']))  # 以s3为间隔符,拼接列表里的每一个元素
    
    

    ---------------------------------------join(用的比较多)一般和split联用----------------------------------------
    234 234 234
    辣条*薯片*汽水*泡面*火腿肠*枸杞*当归*鹿茸

    6.replace

    print(f"{'replace':-^100}")
    s2 = 'yongjiu handsome'
    print(s2.replace('yongjiu', 'gebilaowang'))
    
    

    ----------------------------------------------replace-----------------------------------------------
    gebilaowang handsome

    7.isdigit(纯数字)/isalpha(纯字母)

    print(f"{'isdigit(纯数字)/isalpha(纯字母)':-^100}")
    s2 = '12312'
    print(s2.isdigit())
    
    s3 = 'aaac1c'
    print(s3.isalpha())
    
    

    -------------------------------------isdigit(纯数字)/isalpha(纯字母)--------------------------------------
    True
    False

    3.3 了解(忘了在查)

    1. find|rfind|index|rindex|count
    2. center|ljust|rjust|zfill
    3. expandtabs
    4. captalize|swapcase|title
    5. is系列

    1.find|rfind|index|rindex|count

    print(f"{'find|rfind|index|rindex|count':-^100}")
    s2 = '**23423***xc234234yx $$ hand223423some******'
    #     01234567891011
    print(s2.find('$'))  # 从左找,找到第一个停止,找不到返回-1
    print(s2.rfind('$'))  # 从右找,找到就停止,找不到返回-1
    
    print(s2.index('$'))  # 找不到报错
    print(s2.rindex('$'))  # 找不到报错
    
    

    -----------------------------------find|rfind|index|rindex|count------------------------------------
    21
    22
    21
    22

    2.center|ljust|rjust|zfill

    print(f"{'center|ljust|rjust|zfill':-^100}")
    s2 = 'xucheng handsome'
    print(s2.center(50, '*'))  # 居中
    print(s2.ljust(50, '*'))  # 居左
    print(s2.rjust(50, '*'))  # 居右
    print(s2.zfill(50))  # 填充0居右
    
    

    --------------------------------------center|ljust|rjust|zfill--------------------------------------
    *****************xucheng handsome*****************
    xucheng handsome**********************************
    **********************************xucheng handsome
    0000000000000000000000000000000000xucheng handsome

    3.expandtabs(替换 为需要空格的宽度)

    print(f"{'expandtabs':-^100}")
    s2 = 'a	a'
    print(s2)
    print(s2.expandtabs(8))  # 针对	而言
    
    

    ---------------------------------------------expandtabs---------------------------------------------
    a a
    a a

    4.captalize|swapcase|title 只针对英文

    print(f"{'captalize|swapcase|title  只针对英文':-^100}")
    s2 = 'harry Potter'
    
    print(s2.capitalize())  # 首字母大写,用在段落开始
    print(s2.swapcase())  # 大小写互换
    print(s2.title())  # 所有单词首字母大写
    

    ----------------------------------captalize|swapcase|title 只针对英文-----------------------------------
    Harry potter
    HARRY pOTTER
    Harry Potter

    5.is系列(有兴趣的自己了解)

    4. 存一个值还是多个值

    一个值

    5. 有序or无序

    有序

    6. 可变 or 不可变(重点 可变:值变id不变,不可哈希| 不可变:值变id也变,可哈希)

    不可变

  • 相关阅读:
    tar命令,vi编辑器
    Linux命令、权限
    Color Transfer between Images code实现
    利用Eclipse使用Java OpenCV(Using OpenCV Java with Eclipse)
    Matrix Factorization SVD 矩阵分解
    ZOJ Problem Set
    Machine Learning
    ZOJ Problem Set
    ZOJ Problem Set
    ZOJ Problem Set
  • 原文地址:https://www.cnblogs.com/XuChengNotes/p/11290317.html
Copyright © 2011-2022 走看看