• 字符串str(序列)


    倒序:

    In [1]: a = 'abcd'
    In [5]: a[::-1]
    Out[5]: 'dcba'

     string模块预定义字符串:

    In [1]: import string
    
    In [2]: string.ascii_letters
    Out[2]: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    In [3]: string.ascii_lowercase
    Out[3]: 'abcdefghijklmnopqrstuvwxyz'
    
    In [4]: string.ascii_uppercase
    Out[4]: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    In [5]: string.digits
    Out[5]: '0123456789'

    upper()、lower()、max()、min()、len()

    In [6]: s = 'abCDef'
    
    In [7]: s.upper()
    Out[7]: 'ABCDEF'
    
    In [8]: s.lower()
    Out[8]: 'abcdef'
    
    In [9]: max(s)
    Out[9]: 'f'
    
    In [10]: min(s)
    Out[10]: 'C'
    
    In [11]: len(s)
    Out[11]: 6

    enumerate()、zip()

    In [16]: for i in zip('123','abc'):
        ...:     print(i)
    ('1', 'a')
    ('2', 'b')
    ('3', 'c')
    
    In [17]: for i,j in enumerate('dfgrt',1):
        ...:     print(i,j)
        ...:
    1 d
    2 f
    3 g
    4 r
    5 t

    字符串前加 r 表示原始字符串

    In [18]: st = 'my name is LYB,hello'
    
    In [19]: st.capitalize()  
    Out[19]: 'My name is lyb,hello'
    
    In [21]: st.center(40)
    Out[21]: '          my name is LYB,hello          '
    
    In [22]: st.count('m')
    Out[22]: 2
    
    In [23]: st.encode(encoding='utf-8')
    Out[23]: b'my name is LYB,hello'
    
    In [24]: st.endswith('l')
    Out[24]: False
    
    In [25]: st.endswith('o')
    Out[25]: True
    
    In [26]: st.find('m')  #find和index用法相同,但index没找到报错
    Out[26]: 0
    
    In [27]: st.rfind('m')
    Out[27]: 5
    
    In [28]: st.find('z')
    Out[28]: -1
    
    In [29]: st.index('m')
    Out[29]: 0
    
    In [30]: st.rindex('m')
    Out[30]: 5
    
    In [31]: st.index('z')
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-31-7c17eb92b9c5> in <module>()
    ----> 1 st.index('z')
    
    ValueError: substring not found
    
    In [33]: '123asb'.isalpha()
    Out[33]: False
    
    In [34]: 'asb'.isalpha()
    Out[34]: True
    
    In [36]: '123a sb'.isalnum()
    Out[36]: False
    
    In [37]: '123'.isdigit()
    Out[37]: True
    
    In [38]: st.islower()
    Out[38]: False
    
    In [39]: ''.join('1','2')
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-39-f771cdaf2e91> in <module>()
    ----> 1 ''.join('1','2')
    
    TypeError: join() takes exactly one argument (2 given)
    
    In [40]: ''.join(['1','2'])
    Out[40]: '12'
    
    In [41]: st.isupper()
    Out[41]: False
    
    In [42]: st.strip()
    Out[42]: 'my name is LYB,hello'
    
    In [43]: st.lstrip()
    Out[43]: 'my name is LYB,hello'
    
    In [44]: st.rstrip()
    Out[44]: 'my name is LYB,hello'
    
    In [45]: st.replace(str1='m',str2='5',num=1)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-45-f3785e5ae086> in <module>()
    ----> 1 st.replace(str1='m',str2='5',num=1)
    
    TypeError: replace() takes no keyword arguments
    
    In [46]: st.replace('m','5',1)
    Out[46]: '5y name is LYB,hello'
    
    In [47]: st.replace('m','5',2)
    Out[47]: '5y na5e is LYB,hello'
    
    In [48]: st.split('')
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-48-732f56a6e0f8> in <module>()
    ----> 1 st.split('')
    
    ValueError: empty separator
    
    In [49]: st.split(' ')
    Out[49]: ['my', 'name', 'is', 'LYB,hello']
    
    In [50]: st.splitlines()
    Out[50]: ['my name is LYB,hello']
    
    In [52]: st.swapcase()
    Out[52]: 'MY NAME IS lyb,HELLO'
    
    In [53]: st.startswith('m')
    Out[53]: True
    渐变 --> 突变
  • 相关阅读:
    openstack newton 版本 horizon二次开发
    ubuntu 远程root登录
    记录一次用户态踩内存问题
    (leetcode)二叉树的前序遍历-c语言实现
    通过blacklist来禁用驱动
    最小栈问题
    判断是否为环形链表
    按照层次序列创建二叉树,并判断二叉树是否为二叉搜索树
    操作系统交付时需要做哪些安全检查项
    RDMA相关的技术网站
  • 原文地址:https://www.cnblogs.com/lybpy/p/8145483.html
Copyright © 2020-2023  润新知