字符串
最后更新于:2022-04-02 02:17:36
# 字符串
---
[TOC]
## 基础
### 创建
```python
list1 = ['Google', 'Runoob', 1997, 2000];
#生成平方
[x * x for x in range(1, 11)]
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
```
### 访问
print (list1[0]) #Google
print (list2[2:3])#[1997,2000]
### 更新
list[2] = 2001
### 删除
del list[2]
### 添加
list1.append("word")
## 脚本操作符
### 计算长度
len([1, 2, 3]) -> 3
### 组合
[1, 2, 3] + [4, 5, 6] -> [1, 2, 3, 4, 5, 6]
### 重复
['Hi!'] * 4 -> ['Hi!', 'Hi!', 'Hi!', 'Hi!']
### 判断存在
3 in [1, 2, 3] -> True
### 字符读取
a = 'hello'
a[1] -> e
### 嵌套
a=['a','b','c']
n=[1,2,3]
x=[a,n]
print(x) ->[['a','b','c'],[1,2,3]])
### 筛选
l=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
print(l[::3]) -> [0,3,6,9,12]
### 动态导入模块
try:
import json
except ImportError:
import simplejson
print json.dumps({'python':2.7})
>尝试先导入python自带的json,没有就尝试导入安装的模块simplejson
';