字符串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

 

转载于:https://www.cnblogs.com/lybpy/p/8145483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值