Python中的字符串索引基本知识
2017-03-20 09:51
204 查看
Strings can be indexed (subscripted),
with the first character having index 0. There is no separate character type; a character is simply a string of size one:
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
Indices may also be negative numbers, to start counting from the right:
Note that since -0 is the same as 0, negative indices start from -1.
In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring:
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'Note how the start is always included, and the end always excluded.
This makes sure that
always equal to
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
index defaults to the size of the string being sliced.
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'Python strings cannot be changed — they are immutable.
Therefore, assigning to an indexed position in the string results in an error:
The built-in function
the length of a string:
with the first character having index 0. There is no separate character type; a character is simply a string of size one:
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
Indices may also be negative numbers, to start counting from the right:
>>> word[-1] # last character 'n' >>> word[-2] # second-last character 'o' >>> word[-6] 'P'
Note that since -0 is the same as 0, negative indices start from -1.
In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring:
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'Note how the start is always included, and the end always excluded.
This makes sure that
s[:i] + s[i:]is
always equal to
s:
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second
index defaults to the size of the string being sliced.
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'Python strings cannot be changed — they are immutable.
Therefore, assigning to an indexed position in the string results in an error:
>>> word[0] = 'J' ... TypeError: 'str' object does not support item assignment >>> word[2:] = 'py' ... TypeError: 'str' object does not support item assignment
The built-in function
len()returns
the length of a string:
>>> s = 'supercalifragilisticexpialidocious' >>> len(s) 34
相关文章推荐
- Python中的字符串类型基本知识学习教程
- Python中的字符串类型基本知识学习教程
- 菜鸟的Python之路--基础知识(字符串的基本操作)
- 菜鸟Python之字符串(一):字符串的基本知识
- python字符串基本知识
- python基础知识 (7) ----- 字符串的基本操作
- 简明 Python 教程 第4章 基本概念 字符串
- python基本知识小记1
- SQL Server 索引基础知识(8)--- 数据基本格式补充
- Python基础知识(三)--基本的异常处理、算术运算符、输入/输出
- SQL Server 索引基础知识(8)--- 数据基本格式补充
- [笔记]Python虚拟机的运行时基本知识
- SQL Server 索引基础知识(1)--- 记录数据的基本格式 (转)
- SQL Server 索引基础知识(8)--- 数据基本格式补充(转自蝈蝈俊.net)
- Python基础知识(六)--字符串
- Python基础知识(七)--字符串详解
- SQL Server 索引基础知识(1)--- 记录数据的基本格式(转自蝈蝈俊.net)
- SQL Server 索引基础知识(1)--- 记录数据的基本格式
- SQL Server 索引基础知识(8)--- 数据基本格式补充
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结