美文网首页
04 Python 的基本数据类型----字符串运算

04 Python 的基本数据类型----字符串运算

作者: zackxizi | 来源:发表于2018-05-14 16:40 被阅读0次

1. 字符串拼接

>>> 'hello' + 'world'
'helloworld'
>>> 'hello' * 3
'hellohellohello'
>>>

2. 获取字符串中某个字符

>>> 'hello world'[0]
'h'
>>> 'hello world'[1]
'e'
>>> 'hello world'[2
... ]
'l'
>>> 'hello world'[3]
'l'
>>> 'hello world'[4]
'o'
>>> 'hello world'[5]
' '

获取某个字符,直接输入此字符在字符串中的下标,切记,下标从0开始

>>> 'hello world'[-1]
'd'

注意,str[-n],-n为负数的话,从后面数n个字符

  1. 字符切片
>>> 'Hello World'[6:11]
'World'
>>> 'Hello World'[6:]
'World'
>>> 'Hello Wolrd'[6:-1]
'Wolr'
>>>

使用[初始位置下标:需要切片的到元素的下个位置]

相关文章

网友评论

      本文标题:04 Python 的基本数据类型----字符串运算

      本文链接:https://www.haomeiwen.com/subject/qsgcdftx.html