1. 程式人生 > >Python中的格式化輸出

Python中的格式化輸出

log 帶編號 family color bottom mat int format orm

百分號格式化輸出

  • 百分號默認右對齊

%s

字符串 (采用str()的顯示)

%r

字符串 (采用repr()的顯示)

%c

單個字符

%b

二進制整數

%d

十進制整數

%i

十進制整數

%o

八進制整數

%x

十六進制整數

%e

指數 (基底寫為e)

%E

指數 (基底寫為E)

%f

浮點數

%F

浮點數,與上相同

%g

指數(e)或浮點數 (根據顯示長度)

%G

指數(E)或浮點數 (根據顯示長度)

示例1

name = input(請輸入姓名)
age = input(請輸入年齡)
height = input(請輸入身高)
msg = "我叫%s,今年%s 身高 %s" % (name, age, height)
print(msg)

示例2

name = input(請輸入姓名:)
age = input(請輸入年齡:)
job = input(請輸入工作:)
hobbie = input(你的愛好:)

msg = ‘‘‘------------ info of %s -----------
Name  : %s
Age   : %d
job   : %s
Hobbie: %s
------------- end -----------------‘‘‘ %(name,name,int(age),job,hobbie)
print(msg)

示例3

name = input(請輸入姓名)
age = input(請輸入年齡)
height = input(請輸入身高)
msg = "我叫%s,今年%s 身高 %s 學習進度為3%%s" %(name,age,height)
print(msg)

format格式化輸出

示例1

print({} {}.format(hello,world))    # 不帶編號

示例2

print({0} {1}.format(hello,world))  # 帶數字編號

示例3

print({a} {tom} {a}.format(tom=hello,a=world))  # 帶關鍵字

文章來自選摘

百分號輸出部分來自老男孩視頻

format輸出部分來自:https://www.cnblogs.com/fat39/p/7159881.html

Python中的格式化輸出