2. python 数据类型 格式化

type() 检测数据类型

# list -- 列表
c = [1, 2, 3]
print(type(c))

# tuple -- 元组
d = (1, 2, 3)
print(type(d))

# set -- 集合
e = {10, 20, 30}
print(type(e))

# dict -- 字典   键值对
f = {'name': 'zs', 'age': '19', 'school': '三中'}
print(type(f))


格式化符号


age = 18
name = 'TOM'
weight = 75.5
stu_id = 1

print('我的体重是%.2f' % weight)  # %.2f 默认是保留六位小数、可以通过%.2f来修改保留小数点的位数

print('我的学号是%06d' % stu_id)  # %06d 保留6位数,不足用0来补位,超出部分正常输出

print('我的名字是%s, 年龄%d, 体重%.2f, 学号%06d' % (name, age, weight, stu_id))

print('我的名字是%s, 年龄%s, 体重%s, 学号%s' % (name, age, weight, stu_id)) # %s 的强大之处

# 语法 f'{表达式}'   这种方式更为常用
print(f'我的名字{name},年龄{age}')

转义字符

print('hello\npython')
print('\tabcd')

结束符

print('hello', end='\n')  # 默认
print('world', end='\t')
print('hello', end='...')  # 可以自定义结束符
print('python')
posted @ 2021-11-01 16:17  伴你如风  阅读(46)  评论(0编辑  收藏  举报