基本数据类型
数字 int
字符串 str
布尔值 bool
列表 list
字典 dict
元组 tuple(待续...)
整数 int
- 创建
a = 123
a = int(123)
- 转换
age = "18"
new_age = int(age)
布尔值
- 创建
a = True
b = False
- 转换
- 数字转换,只有0是False,其他True
- 字符串, 只有""是False,其他True
- 待续...
字符串
- 创建- 转换
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 a = "alex" 2 a = str('alex')
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 age = 19 2 new_age = str(age)
- 字符串的拼接
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 name = 'alex' 2 gender = '女' 3 new_str = name + gender 4 print(new_str)
- 字符串格式化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 占位符, 2 name = '我叫李杰,性别:%s,我今年%s岁,我在说谎!' 3 new_str = name %('男',19,) 4 print(new_str 5 name = '我叫李杰,性别:%s,我今年%s岁,我在说谎!' %('男',19,) 6 print(name) 7 待续......
- 判断子序列是否在其中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 content = "Alex 前几天去泰国玩姑娘,一不小心染上了病,他的内心活动是,真该多来几个" 2 3 if "前几天去" in content: 4 print('包含敏感字符') 5 else: 6 print(content)
- 移除空白
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 val = " alex " 2 print(val) 3 new_val = val.strip() # 左右 4 new_val = val.lstrip()# 左边 5 new_val = val.rstrip() # 右边 6 print(new_val)
- 分割
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 user_info = "alex sb123 9" 2 v = user_info.split('|') 3 v = user_info.split('|',1) 4 v = user_info.rsplit(' ',1) 5 print(v)
- 长度(字符)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 1 val = "左边sb" 2 2 v = len(val) 3 3 print(v)
- 索引
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 val = "左边sb" 2 v = val[0] 3 4 print(v) 5 6 val = input('>>>') 7 i = 0 8 while i < len(val): 9 print(val[i]) 10 i += 1
- 切片
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 name = '我叫左边,性别我今年岁,我在说谎!' 2 print(name[0]) 3 print(name[0:2]) 4 print(name[5:9]) 5 print(name[5:]) 6 print(name[5:-2]) 7 print(name[-2:])