• 字符串拼接

>>> 'hello'+'world'
'helloworld'
>>> 'hello'*3
'hellohellohello'
>>> 'hello'*'world'
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    'hello'*'world'
TypeError: can't multiply sequence by non-int of type 'str'
  •  字符串截取

>>> 'hello world'
'hello world'
>>> 'hello world'[0]
'h'
>>> 'hello world'[3]
'l'
>>> 'hello world'[4]
'o'
>>> 'hello world'[-1]
'd'
>>> 'hello world'[-5]
'w'
>>> 'hello world'[6]
'w'

 

>>> 'hello world'[0:4]
'hell'
>>> 'hello world'[0:5]
'hello'
>>> 'hello world'[0:-1]
'hello worl'
>>> 'hello world'[0:-3]
'hello wo'
>>> 'hello world'[6:10]
'worl'
>>> 'hello world'[6:11]
'world'
>>> 'hello world'[6:21]
'world'
>>> 'hello world'[6:-1]
'worl'
>>> 'hello world'[6:0]
''
>>> 'hello world'[6:]
'world'
>>> 'hello world'[:-1]
'hello worl'
>>> 'hello world'[:-4]
'hello w'
>>> 'hello world'[:-6]
'hello'
>>> 'hello world'[-5:]
'world'

 

最后修改于 2019-04-06 19:28:10
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇