python怎么让输出的数字右对齐_python(格式化输出)
一、%格式化輸出
1、整數(shù)的輸出(參照ASCII)
%o —— oct 八進(jìn)制
%d —— dec 十進(jìn)制(digit )
%x —— hex 十六進(jìn)制
>>> print('%o' % 20)
24
>>> print('%d' % 20)
20
>>> print('%x' % 20)
14
2、浮點(diǎn)數(shù)輸出
%f —— float 保留小數(shù)點(diǎn)后面六位有效數(shù)字
%.3f,保留3位小數(shù)位
%e —— 保留小數(shù)點(diǎn)后面六位有效數(shù)字,指數(shù)形式輸出
%.3e,保留3位小數(shù)位,使用科學(xué)計(jì)數(shù)法
%g —— 在保證六位有效數(shù)字的前提下,使用小數(shù)方式,否則使用科學(xué)計(jì)數(shù)法
%.3g,保留3位有效數(shù)字,使用小數(shù)或科學(xué)計(jì)數(shù)法
>>> print('%f' % 1.11) # 默認(rèn)保留6位小數(shù)
1.110000
>>> print('%.1f' % 1.11) # 取1位小數(shù)
1.1
>>> print('%e' % 1.11) # 默認(rèn)6位小數(shù),用科學(xué)計(jì)數(shù)法
1.110000e+00
>>> print('%.3e' % 1.11) # 取3位小數(shù),用科學(xué)計(jì)數(shù)法
1.110e+00
>>> print('%g' % 1111.1111) # 默認(rèn)6位有效數(shù)字
1111.11
>>> print('%.7g' % 1111.1111) # 取7位有效數(shù)字
1111.111
>>> print('%.2g' % 1111.1111) # 取2位有效數(shù)字,自動(dòng)轉(zhuǎn)換為科學(xué)計(jì)數(shù)法
1.1e+03
3、字符串輸出
%s?—— string 字符串
%10s —— 右對(duì)齊,占位符10位
%-10s —— 左對(duì)齊,占位符10位
%.2s —— 截取2位字符串
%10.2s —— 10位占位符,截取兩位字符串
>>> print('%s' % 'hello world') # 字符串輸出
hello world
>>> print('%20s' % 'hello world') # 右對(duì)齊,取20位,不夠則補(bǔ)位
hello world
>>> print('%-20s' % 'hello world') # 左對(duì)齊,取20位,不夠則補(bǔ)位
hello world
>>> print('%.2s' % 'hello world') # 取2位
he
>>> print('%10.2s' % 'hello world') # 右對(duì)齊,取2位
he
>>> print('%-10.2s' % 'hello world') # 左對(duì)齊,取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() 該函數(shù)把字符串當(dāng)成一個(gè)模板,通過傳入的參數(shù)進(jìn)行格式化,并且使用大括號(hào) "{ }" 作為特殊字符代替 "%"
{ } 中不設(shè)參數(shù)
>>>"{} {}".format("hello", "world") # 不設(shè)置指定位置,按默認(rèn)順序
'hello world'
>>> "{0} {1}".format("hello", "world") # 設(shè)置指定位置
'hello world'
>>> "{1} {0} {1}".format("hello", "world") # 設(shè)置指定位置
'world hello world'
{ } 中設(shè)置參數(shù)
# 通過變量設(shè)置參數(shù)
print("My name is {name},and I am {age} years old!".format(name = "zhangsan",age = "25"))
# 通過字典設(shè)置參數(shù)
info = {"name": "zhangsan", "age": "25"}
print("My name is {name},and I am {age} years old!".format(**info))
# 通過列表索引設(shè)置參數(shù)
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() 格式化數(shù)字
^,?<,?>?分別是居中、左對(duì)齊、右對(duì)齊,后面帶寬度,?:?號(hào)后面帶填充的字符,只能是一個(gè)字符,不指定則默認(rèn)是用空格填充。
+?表示在正數(shù)前顯示?+,負(fù)數(shù)前顯示?-;??(空格)表示在正數(shù)前加空格
>>> print("{:.2f}".format(3.1415926));
3.14
數(shù)字格式輸出描述
3.1415926
{:.2f}
3.14
保留小數(shù)點(diǎn)后兩位
3.1415926
{:+.2f}
+3.14
帶符號(hào)保留小數(shù)點(diǎn)后兩位
-1
{:+.2f}
-1.00
帶符號(hào)保留小數(shù)點(diǎn)后兩位
2.71828
{:.0f}
3
不帶小數(shù)
5
{:0>2d}
05
數(shù)字補(bǔ)零 (填充左邊, 寬度為2)
5
{:x<4d}
5xxx
數(shù)字補(bǔ)x (填充右邊, 寬度為4)
10
{:x<4d}
10xx
數(shù)字補(bǔ)x (填充右邊, 寬度為4)
1000000
{:,}
1,000,000
以逗號(hào)分隔的數(shù)字格式
0.25
{:.2%}
25.00%
百分比格式
1000000000
{:.2e}
1.00e+09
指數(shù)記法
13
{:>10d}
13
右對(duì)齊 (默認(rèn), 寬度為10)
13
{:<10d}
13
左對(duì)齊 (寬度為10)
13
{:^10d}
13
中間對(duì)齊 (寬度為10)
注意:可以使用大括號(hào)?{}?來轉(zhuǎn)義大括號(hào)
print ("{}今年{{25}}歲了".format("張三"))
標(biāo)簽:輸出,格式化,name,format,python,對(duì)齊,print,world,hello
來源: https://www.cnblogs.com/Mr-ZY/p/12104357.html
總結(jié)
以上是生活随笔為你收集整理的python怎么让输出的数字右对齐_python(格式化输出)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Control 2》游戏进入概念验证阶
- 下一篇: python中定义字符串_Python中