玩命加载中...
### 3. 读取字符串中的字符
读取字符串中的字符与读取list中的元素的操作是一样的。
```python
my_str = 'Hello SofaSofa!'
# 读取my_str中的第一个字符
a = my_str[0]
# 读取my_list中的第三个元素
b = my_str[2]
```
也可以反向读取字符串中的字符。
```python
# 读取my_str中的最后一个字符
c = my_str[-1]
print c
# 读取my_str中的倒数第二个字符
d = my_str[-2]
print d
```
我们可以用`:`读取string中的多个字符。基本用法与`list`相同。
```python
my_str = 'Hello SofaSofa!'
# 从左向右输出前7个字符
print my_str[:7]
# 从右往左输出后4个元素
print my_str[-4:]
```
上面是读取连续的字符,也可以读取非连续的字符。基本用法与`list`相同。
```python
my_str = 'Hello SofaSofa!'
# 输出第2,4,6,8个字符
a = my_str[1:8:2]
print a
```
利用`:`和`-1`,我们可以将整个字符串取逆。
```python
my_str = 'Hello SofaSofa!'
a = my_str[::-1]
print a
#试试看下面返回什么呢?
a = my_str[::-2]
print a
```