python格式化輸入包含'%'呼叫,及format方法
1#最簡單的字串傳參
2 tpl='
i am %s
'%'alex
'3 >>>i am alex 45
#字串與整數傳參
6 tpl = '
i am %s age %d
'%('
alex
',18)
7 >>>i am alex age 1889
#字典的傳參
10 tpl = '
i am %(name)s age %(age)d
'% 11 >>>i am alex age 18
1213
#浮點型傳參
14 tpl = '
percent %.2f
'%99.97623
15 >>>percent 99.98
1617
#字典中的浮點呼叫
18 tpl = '
i am %(pp).2f
'%19 >>>i am 123.43
2021
#注意點:表示百分號的時候需要加個%
22 tpl = '
i am %.2f %%
'%123.425556
23 >>>i am 123.43 %
2425
#錯誤的方式
26 tpl = '
i am %.2f %
'%123.425556
27 >>> tpl = '
i am %.2f %
'%123.425556
28 >>>valueerror: incomplete format
1#format方法順序對應傳參
2 tpl = '
i am {},age{},{}
'.format('
serven
',18,'
alex')
3 >>>i am serven,age18,alex45
#format列表傳參*號為將列表分割後順序傳入,不加則會將整個傳入而報錯
6 tpl = '
i am {},age{},{}
'.format(*['
serven
',18,'
alex'])
7 >>>i am serven,age18,alex89
#format標序傳參
10 tpl = '
i am ,age ,really
'.format('
serven
',18)
11 >>>i am serven,age 18,really serven
1213
#format指定引數名傳參
14 tpl = '
i am ,age ,really
'.format(name='
seven
',age=18)
15 >>>i am seven,age 18,really seven
1617
#format標序後指定下標傳參
18 tpl = '
i am ,age,really
'.format([1,2,3],[11,22,33])
19 >>>i am 1,age2,really3
2021
#對應順序傳參,f整數轉為浮點型小數點後保留6位
22 tpl = '
i am ,age,money
'.format('
seven
',18,88888.1)
23 >>>i am seven,age18,money 88888.100000
2425
#format 字典傳參
26 tpl = '
i am ,age
'.format(**)
27 >>>i am seven,age18
2829
#:b轉化為2進製,:o轉化為8進製:x轉化為16進製制小寫,:x轉化為16進製制大寫:%將值轉化為百分比
30 tpl = '
numbers : ,,,,,
'.format(15,15,15,15,15,15.87623,2)
31 >>>numbers : 1111,15,15,f,f,1587.623000%
1 格式化輸入輸出
1.格式化輸入input input 函式,通常只能返回乙個資料型別,那麼怎麼可以進行多個變數的複製呢?看下面這段 1 str1,str2 eval input 請輸入兩個字串 2 print str1,str2 3 4 num1,num2 eval input 請輸入兩個數字 5 print nu...
python day02 格式化輸出邏輯運算
格式化輸出 s d name input 請輸入姓名 age input 請輸入年齡 height input 請輸入身高 msg 我叫 s,今年 s 身高 s name,age,height print msg name input 請輸入姓名 age input 請輸入年齡 job input ...
58 格式化輸入輸出練習
lesson 58.格式化輸入輸出練習 python格式化輸入輸出的練習 要求如下 求平均成績 python3直譯器 1 輸入學生姓名 2 依次輸入學生的語文,數學,英語三門科目成績 3 計算該學生的平均成績並列印,平均成績保留一位小數點 4 計算該學生語文成績佔總成績的百分之多少?並列印,百分比保...