#範例15-3:文字的格式化顯示:format(),可處理文字+(整數,小數點,文字) #https://www.w3schools.com/python/python_datetime.asp #https://www.w3school.com.cn/python/python_strings.asp #https://www.runoob.com/python3/python3-string.html #1.----- 文字 + 整數,會發生錯誤 --------- a1 = "我的年紀是" #a2 = a1 + 20 #print(a1) #2.----- 文字+整數的解決方法1:文字+str(整數) ------- print() a1 = "我的年紀是" #3.----- 文字+整數的解決方法2:使用 a1.format(20) 組合文字與數字 ---- print() a1 = "我的年紀是{}" #4.----- 文字+整數的解決方法2(多參數):a1.format(170, 60.5, "john") 組合文字與數字 --- print() a1 = "我的身高是{}, 體重是{},姓名是{}" #5.----- 文字+整數的解決方法3(新版f.string格式化): f"文字{變數}" --- print() a1 = 21 h1 = 170 h2 = 60 h3 = "mike" #------------------------------------------------------------------------- #------------------------------------------------------------------------- #範例15-3:文字的格式化顯示:format(),可處理文字+(整數,小數點,文字) #https://www.w3schools.com/python/python_strings_format.asp #https://www.w3school.com.cn/python/python_strings.asp #https://www.runoob.com/python3/python3-string.html #1.----- 文字 + 整數,會發生錯誤 --------- a1 = "我的年紀是" #a2 = a1 + 20 #print(a1) #2.----- 文字+整數的解決方法1:文字+str(整數) ------- print() a1 = "我的年紀是" a2 = a1 + str(20) print(a2) #3.----- 文字+整數的解決方法2:使用 a1.format(20) 組合文字與數字 ---- print() a1 = "我的年紀是{}" print(a1.format(20)) #4.----- 文字+整數的解決方法2(多參數):a1.format(170, 60) 組合文字與數字 --- print() a1 = "我的身高是{}, 體重是{},姓名是{}" print(a1.format(170, 60.5, "john")) #5.----- 文字+整數的解決方法3(新版f.string格式化): f"文字{變數}" --- print() a1 = 21 a2 = f"我的年紀是{a1}" h1 = 170 h2 = 60 h3 = "mike" a2 = f"我的身高是{h1}, 體重是{h2},姓名是{h3}" print(a2) #-------------------------------------------------------------------------