python怎么让输出的数字右对齐_python(格式化输出)
一、%格式化输出
1、整数的输出(参照ASCII)
%o —— oct 八进制
%d —— dec 十进制(digit )
%x —— hex 十六进制
>>> print('%o' % 20)
24
>>> print('%d' % 20)
20
>>> print('%x' % 20)
14
2、浮点数输出
%f —— float 保留小数点后面六位有效数字
%.3f,保留3位小数位
%e —— 保留小数点后面六位有效数字,指数形式输出
%.3e,保留3位小数位,使用科学计数法
%g —— 在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
%.3g,保留3位有效数字,使用小数或科学计数法
>>> print('%f' % 1.11) # 默认保留6位小数
1.110000
>>> print('%.1f' % 1.11) # 取1位小数
1.1
>>> print('%e' % 1.11) # 默认6位小数,用科学计数法
1.110000e+00
>>> print('%.3e' % 1.11) # 取3位小数,用科学计数法
1.110e+00
>>> print('%g' % 1111.1111) # 默认6位有效数字
1111.11
>>> print('%.7g' % 1111.1111) # 取7位有效数字
1111.111
>>> print('%.2g' % 1111.1111) # 取2位有效数字,自动转换为科学计数法
1.1e+03
3、字符串输出
%s —— string 字符串
%10s —— 右对齐,占位符10位
%-10s —— 左对齐,占位符10位
%.2s —— 截取2位字符串
%10.2s —— 10位占位符,截取两位字符串
>>> print('%s' % 'hello world') # 字符串输出
hello world
>>> print('%20s' % 'hello world') # 右对齐,取20位,不够则补位
hello world
>>> print('%-20s' % 'hello world') # 左对齐,取20位,不够则补位
hello world
>>> print('%.2s' % 'hello world') # 取2位
he
>>> print('%10.2s' % 'hello world') # 右对齐,取2位
he
>>> print('%-10.2s' % 'hello world') # 左对齐,取2位
he
name = input("Name:")
age = int(input("Age:"))
job = input("Job:")
salary = int(input("Salary:"))
msg = """
------------info of %s------------
Name:%s
Age:%d
Job:%s
Salary:%d
----------------end---------------
"""%(name,name,age,job,salary)
print(msg)
二、format 格式化
str.format() 该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号 "{ }" 作为特殊字符代替 "%"
{ } 中不设参数
>>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
'hello world'
>>> "{0} {1}".format("hello", "world") # 设置指定位置
'hello world'
>>> "{1} {0} {1}".format("hello", "world") # 设置指定位置
'world hello world'
{ } 中设置参数
# 通过变量设置参数
print("My name is {name},and I am {age} years old!".format(name = "zhangsan",age = "25"))
# 通过字典设置参数
info = {"name": "zhangsan", "age": "25"}
print("My name is {name},and I am {age} years old!".format(**info))
# 通过列表索引设置参数
msg = ["zhangsan","25"]
print("My name is {0[0]},and I am {0[1]} years old!".format(msg))
---> My name is zhangsan,and I am 25 years old!
---> My name is zhangsan,and I am 25 years old!
---> My name is zhangsan,and I am 25 years old!
str.format() 格式化数字
^, <, > 分别是居中、左对齐、右对齐,后面带宽度, : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
+ 表示在正数前显示 +,负数前显示 -; (空格)表示在正数前加空格
>>> print("{:.2f}".format(3.1415926));
3.14
数字格式输出描述
3.1415926
{:.2f}
3.14
保留小数点后两位
3.1415926
{:+.2f}
+3.14
带符号保留小数点后两位
-1
{:+.2f}
-1.00
带符号保留小数点后两位
2.71828
{:.0f}
3
不带小数
5
{:0>2d}
05
数字补零 (填充左边, 宽度为2)
5
{:x<4d}
5xxx
数字补x (填充右边, 宽度为4)
10
{:x<4d}
10xx
数字补x (填充右边, 宽度为4)
1000000
{:,}
1,000,000
以逗号分隔的数字格式
0.25
{:.2%}
25.00%
百分比格式
1000000000
{:.2e}
1.00e+09
指数记法
13
{:>10d}
13
右对齐 (默认, 宽度为10)
13
{:<10d}
13
左对齐 (宽度为10)
13
{:^10d}
13
中间对齐 (宽度为10)
注意:可以使用大括号 {} 来转义大括号
print ("{}今年{{25}}岁了".format("张三"))
标签:输出,格式化,name,format,python,对齐,print,world,hello
来源: https://www.cnblogs.com/Mr-ZY/p/12104357.html
python怎么让输出的数字右对齐_python(格式化输出)相关推荐
- python怎么让输出的数字右对齐_Python格式化输出的精度和位数控制
原创作者:牵引小哥 微信公众号:牵引小哥讲Python 注:转载或复制请注明出处--牵引小哥 上一期小哥讲解了Python格式化输出的三种方式. 本期小哥讲解一下关于小数精度和字符串位数的控制: 1. ...
- python中格式化输出的作用是什么_python格式化输出是什么意思
我们经常会输出类似'亲爱的xxx你好!你xx月的话费是xx,余额是xx'之类的字符串',而xxx的内容都是根据变量变化的,所以,需要一种简便的格式化字符串的方式.格式化输出就可以帮助你实现这个功能. ...
- c++输出小数点后几位_Python格式化输出的精度和位数控制
上一期小哥讲解了Python格式化输出的三种方式: 本期小哥讲解一下关于小数精度和字符串位数的控制: 1. 小数精度控制 Python中对于小数精度的控制的使用方法: # 定义变量a = 3.1415 ...
- c++输出小数点后几位_Python格式化输出:%s和format()用法比较
1.python格式化输出历史起源 python2.5之前,我们使用的是老式格式化输出:%s.从python3.0开始起(python2.6同期发布),同时支持两个版本的格式化,多出来的一个新版本就是 ...
- python表格对齐_python 格式化输出 列表对齐左对齐右对齐
原博文 2020-10-14 09:30 − python对齐输出 https://www.cnblogs.com/nul1/p/11136495.html python - 输出列表自动对齐(支持中 ...
- python的format用法打印表格_Python格式化输出——format用法示例
format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {} ...
- python数据的格式输出_Python格式化输出
"%"的使用 格式符 描述 %s 字符串 (采用str()的显示) %r 字符串 (采用repr()的显示) %c 单个字符及其ASCII码 %u 整数(无符号) %b 二进制整数 ...
- python format左对齐_Python_format格式化输出基础篇
今天分享一下Python的format格式化输出的一些基本函数,什么叫做format格式化输出? 相对基本格式化输出采用%号的方法,format()功能更加强大,该函数把字符串当成一个模板,通过传入的 ...
- python九九乘法表右对齐_python语法练习题之九九乘法表
九九乘法表 for...in方法实现 #方法一 for i in range(1, 10):for j in range(1, i+1):print('{}*{}={:<4}'.format(j ...
最新文章
- 反反复复的磁盘丢失故障处理过程
- VB winform自动更新 笔记
- sap.ui.require in SAP UI5 and require in nodejs
- Leetcode 648.单词替换
- clickhouse语句_篇四|ClickHouse的可视化界面与集群状态监控
- MEncoder 使用实例
- 蚂蚁集团SOFAStack:新一代分布式云PaaS平台,打造企业上云新体验
- BZOJ 3261: 最大异或和 [可持久化Trie]
- Python小程序(3)--BMR(基础代谢率)计算器
- CefSharp入门-winform
- MFC API——》ModifyStyle
- 什么是Base64 编码,Base64 编码有哪些优缺点?
- 你真的打算凑合过完这一生吗(转)
- 【转】艺术设计、数字媒体、环艺、影视动画、摄影、广编专业…等…视频、教程、资讯、图库、作品汇总大全
- Git GitHub Gitee使用方法
- P30有ROOT吗,华为p30能root吗
- lua系统学习02-变量
- ESP8266实现WIFI局域网连接手机APP监控
- Logism · 汉字字库存储芯片扩展 实验
- java 显示日历 swing_Java Swing 日历 控件
热门文章
- C#操作SQLite数据库增、删、改、查 实例
- Qt Widgets、QML、Qt Quick的概念与区别
- 自动化设备的软件框架
- C#中的bitmap类和图像像素值获取方法
- java 远程修改linux服务器文件_linux服务器之间复制文件
- kafka时间轮linux时间轮,Kafka解惑之时间轮 (TimingWheel)
- python即时标记_【Python】读书笔记:Python基础教程-项目1-即时标记
- Qt定时器QBasicTimer、startTimer、QTimer使用总结
- QImage、 QPixmap、 QBitmap、 QPicture区别
- C++11线程管理基础