1. 程式人生 > >python | 輸入與輸出 | 格式符

python | 輸入與輸出 | 格式符

pytho txt content 小數 python3 con eva pan 文件中

 1 # encoding: utf-8
 2 
 3 # python3中的input相當於python2中的raw_input(未加工)
 4 content = input(請輸入內容,該內容被當做字符串處理:)
 5 print(content)
 6 
 7 # 也可以將input的內容當做代碼來處理
 8 content = eval(input(請輸入內容,該內容被當做代碼處理:))
 9 print(content)
10 
11 # 格式化輸出
12 name = marry
13 age = 18
14 print(我的姓名是:{0},我的年齡是:{1}.format(name, age))
15 print(我的姓名是:%s,我的年齡是:%d % (name, age)) 16 17 # 輸出到文件中 18 f = open("test.txt", "w") 19 print("hello word", file=f) 20 21 # 輸出不自動換行 22 print("hello word", end="") 23 24 # 添加分隔符 25 print("1", "2", "3", sep=",") 26 27 # 立即輸出 28 # 如果待輸出的內容有換行,則會立即輸出,不會在緩沖區逗留 29 # 如果沒有換行,則不會立即輸出 30 # 解決方案是flush = True
31 print("hello word", flush=True) 32 33 # 格式符 34 grade = 89 35 print("%10d" % grade) 36 print("%-10d" % grade) 37 print("% d" % grade) 38 39 # 時鐘表示 40 m = 5 41 s = 8 42 print("%02d:%02d" % (m, s)) 43 44 # 小數點精度 45 f = 43.5 46 print("%f" % f) 47 print("%.2f" % f) 48 49 # 轉化為八進制 50 print("%o" % 100) 51 52
# 轉換為十六進制 53 print("%x" % 100) 54 55 # 科學計數法表示 56 print("%e" % 1000000000) 57 print("%E" % 1000000000) 58 59 # 自動轉換為整數或小數或科學計數法(超過六位時)表示 60 print("%g" % 23) 61 print("%g" % 23.34) 62 print("%g" % 12000000000) 63 64 # 將數字轉換為其unicode對應的值 65 print("%c" % 19997) 66 67 # 百分數表示 68 # 用%%轉義% 69 grade = 89 70 print("%d%%" % 89)

python | 輸入與輸出 | 格式符