- ''''''
- '''
- 基本数据类型:
- 数字类型:
- 1, 整型: int
- 人的年龄, 身份 ID 号...
- 2, 浮点型: float
- 人的身高体重, 薪资
- '''
- #int
- age = int(18)
- print(age)
- print(type(age))
- age2 = 19 #会自动识别类型
- print(age)
- print(type(age))
- #float
- sal = 1.01
- print(sal)
- print(type(sal))
- '''
- 字符串类型:
- str
- 作用:
- 名字, 性别, 国籍, 地址等描述信息
- 定义:
- 在单引号, 双引号, 三引号内, 有一串字符组成
- '''
- # 单引号
- str1 = '你好'
- print(str1)
- print(type(str1))
- # 双引号
- str2 = "你好"
- print(str2)
- print(type(str2))
- # 三引号: 可以写多行
- str3 = '''
- 安徽省
- 合肥市
- 合肥学院
- '''
- print(str3)
- print(type(str3))
- '''
- 优先掌握的操作:
- 1, 按索引取值(正向取 + 反向取): 只能取
- 2, 切片(顾头不顾尾, 步长)
- 3, 长度 len
- 4, 成员运算 in 和 not in
- 5, 移除空白 strip
- 6, 切分 split
- 7, 循环
- '''
- # 按索引取值(正向取 + 反向取): 只能取
- # 正向取
- str1 = 'hello tank!'
- print(str1[0]) #打印 h
- print(str1[9]) #打印 k
- # 反向取
- print(str1[-2]) # k
- # 切片(顾头不顾尾, 步长)
- str1 = 'hello tank!'
- print(str1[0:4]) #打印 hell
- # 步长
- print(str1[0:11]) #hello tank!
- print(str1[0:11:2]) #hlotn!
- # 长度 len
- print(len(str1)) #11
- # 成员运算 in 和 not in
- print('h'in str1) #True
- print('h'not in str1) #False
- # 移除空白 strip
- # 会移除字符串中左右两边的空格
- str1 = 'hello tank!'
- print(str1)
- str1 ='hello tank!'
- print(str1)
- print(str1.strip())
- # 去除指定字符串
- str2 = '!tank!'
- print(str2.strip('!'))
- # 切分 split
- str1='hello tank!'
- # 根据 str1 内的空格进行切分
- # 切分出来的值会存在 [] 列表中
- print(str1.split('')) #['hello','tank!']
- # 循环
- # 对 str1 字符串进行遍历, 打印每一个人字符
- for line in str1:
- print(line)
- '''
- 字符串类型
- 需要掌握的
- '''
- #1,strip,lstrip,rstrip
- str1 = 'hello yaya'
- print(str1)
- # 去掉两边空格
- print(str1.strip())
- # 去掉左边空格
- print(str1.lstrip())
- # 去掉右边空格
- print(str1.rstrip())
- #2,lower,upper
- str1 = 'hello YaYa'
- # 转换成小写
- print(str1.lower())
- # 转换成大写
- print(str1.upper())
- #3,startswith\endswith
- str1 = 'hello yaya'
- # 判断 str1 开头是否为 hello
- print(str1.startswith('hello')) #True
- # 判断 str1 字符末尾是否等于 yaya
- print(str1.endswith('yaya')) #True
- #4,format(格式化输出)的三种玩法
- str1 = 'my name is %s, my age %s!' %('tank',18)
- print(str1)
- # 方式一: 根据位置顺序格式化
- print('my name is {}, my age {}!'.format('tank',18))
- # 方式二: 根据索引格式化
- print('my name is {0}, my age {1}!'.format('tank',18))
- # 方式三: 指名道姓的格式化
- print('my name is {name}, my age {age}!'.format(age=18,name='tank'))
- #5,split,lspilt
- str1 = 'hello tank!'
- print(str1.split(' '))
- #print(str1.lsplit(' ')) #????
- #6,join 字符串拼接
- print(''.join(['tank',18])) #报错
- # 把列表中的每一个字符串进行拼接
- print(''.join(['tank','18']))
- # 根据空格, 把列表中的每一个字符串进行拼接
- print(''.join(['tank','18','from GZ']))
- # 根据_, 把列表中的每一个字符串进行拼接
- print('_'.join(['tank','18','from GZ']))
- #7,replace: 字符串替换
- str1 = 'my name is yaya, my age 18'
- print(str1)
- str2 = str1.replace('yaya','qy')
- print(str2)
- #8,isdigit: 判断字符串是否是数字
- choice = input('请选择功能[0,1,2]:')
- # 判断用户输入的选择是否是数字
- print(choice.isdigit())
- #9,index: 输出索引位置
- str1 = 'hello yaya'
- print(str1.index('o'))
来源: http://www.bubuko.com/infodetail-3102729.html