西杰网站建设,自贡做响应式网站开发公司,国家企业官网查询系统,企业网站优化打印 PRINT 字符串和数值类型 可以直接输出。 print(1) #out:1
print(a) #out:a 变量 无论什么类型#xff0c;数值#xff0c;字符串#xff0c;列表#xff0c;字典...都可以直接输出 n 1
s a
list_a [1,3,4]
dict_c {a:3,b:4}
print(n) #outa) #out:a 变量 无论什么类型数值字符串列表字典...都可以直接输出 n 1
s a
list_a [1,3,4]
dict_c {a:3,b:4}
print(n) #out1
print(s) #outa
print(list_a) #out[1, 3, 4]
print(dict_c) #out:{a: 3, b: 4} 格式化输出 字符串格式控制%[(name)][flag][width][.][precision]type name:可为空数字(占位),命名(传递参数名,不能以数字开头)以字典格式映射格式化其为键名 flag:标记格式限定符号,包含-#和0,表示右对齐(会显示正负号),-左对齐,前面默认为填充空格(即默认右对齐)0表示填充0#表示八进制时前面补充0,16进制数填充0x,二进制填充0b width:宽度(最短长度,包含小数点,小于width时会填充) precision:小数点后的位数,与C相同 type:输入格式类型请看下面 %字符标记转换说明符的开始 格式描述%%百分号%标记%s字符串%c字符及其ASCLL码%d有符号整数(十进制)%u无符号整数(十进制)%f浮点数字(用小数点符号)%g浮点数字(根据值的大小采用%e或%f)%G浮点数字(类似于%g)%o无符号整数(八进制)%x无符号整数(十六进制)%X无符号整数(十六进制大写字符)%e浮点数字(科学计数法)%E浮点数字(科学计数法用E代替e)%p指针(用十六进制打印值的内存地址)%n存储输出字符的数量放进参数列表的下一个变量中 实例 print(打印字符串%s%(hello world)) #out 打印字符串hello world
print(打印带符号十进制整数%d %(-25)) #out 打印带符号十进制整数-25
print(打印限定宽度带符号十进制整数%02d%(5)) #out 打印左对齐字符串05
print(打印限定宽度带符号十进制整数%02d%(125)) #out 打印左对齐字符串125
print(打印无符号十进制整数%u %(-25)) #out 打印无符号十进制整数-25
print(打印浮点数%f %(-25)) #out 打印无符号十进制整数-25.000
print(打印带精度的浮点数%0.3f %(-25)) #out 打印无符号十进制整数-25.000
print(打印左对齐字符串%-15s%(hello world)) #out 打印字符串hello world
print(打印左对齐字符串%15s%(hello world)) #out 打印左对齐字符串 hello world
print(打印字符串%015s%(hello world)) #out 打印字符串 hello world
print(打印带精度的科学计数法%.0e%(1000)) #out 打印带精度的科学计数法1e03
print( I am %s and age is %d %(qsl,25) ) #out I am qsl and age is 25 format_spec格式{[name][:][[fill]align][sign][#][0][width][,][.precision][type]} 用{}包裹name命名传递给format以命名值 写法,非字典映射,其他和上面相同 fill any character #fill是表示可以填写任何字符 align | | | ^ #align是对齐方式是左对齐 是右对齐^是居中对齐。 sign | - | #sign是符号 表示正号 -表示负号 width integer #width是数字宽度表示总共输出多少位数字 precision integer #precision是小数保留位数 type b | c | d | e | E | f | F | g | G | n | o | s | x | X | % #type是输出数字值是的表示方式比如b是二进制表示比如E是指数表示比如X是十六进制表示 参考https://www.cnblogs.com/lovejh/p/9201219.html 基本用法 # 不带字段
print({} {}.format(hello,world)) #out:hello world
# 带数字编号
print({0} {1}.format(hello,world)) #out:hello world
# 打乱顺序
print({0} {1} {0}.format(hello,world)) #out:hello world hello
print({1} {1} {0}.format(hello,world)) #out:world world hello
# 带关键字
print({a} {tom} {a}.format(tomhello,aworld)) #out:world hello world 转载于:https://www.cnblogs.com/qianslup/p/11088867.html