用Notepad++打下列的程式碼,
另存為ex9.py.
# Here's some new strange stuff, remember type it exactly. days = "Mon Tue Wed Thu Fri Sat Sun" months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" print ("Here are the days: ", days) print ("Here are the months: ", months) print (""" There's something going on here. with the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6. """)
然後用Windows的cmd, 執行python打開它.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW C:\Users\Peter\Desktop\Python\LP3THW>python .\ex9.py Here are the days: Mon Tue Wed Thu Fri Sat Sun Here are the months: Jan Feb Mar Apr May Jun Jul Aug There's something going on here. with the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6. C:\Users\Peter\Desktop\Python\LP3THW>
完成
這次練習主要是學習對字符串做更多複雜的格式化.
我們來修改一下程式碼.
Line 7 - 8, 我用兩種不同的格式化表達方式, 使其出現相同的結果.
Line 11 & 16, 主要是將"號 與 '號替換 但結果相同.
Line 12, 開頭加了一個TAB, 其結果也是TAB了.
Line 14, \n 是跳行.
Line 15, \\是表示\.
# Here's some new strange stuff, remember type it exactly. days = "Mon Tue Wed Thu Fri Sat Sun" months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" print ("Here are the days: ",days) print (f"Here are the days: {days}") print ("Here are the days: {}".format(days)) print ("Here are the months: ", months) print (''' There's something going on here. with the three double-quotes. We'll be able to type as much as we like.\n Even 4 lines if we want, or 5, or 6.\\ ''')
結果,
C:\Users\Peter\Desktop\Python\LP3THW>python .\ex9.py Here are the days: Mon Tue Wed Thu Fri Sat Sun Here are the days: Mon Tue Wed Thu Fri Sat Sun Here are the days: Mon Tue Wed Thu Fri Sat Sun Here are the months: Jan Feb Mar Apr May Jun Jul Aug There's something going on here. with the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6.\ C:\Users\Peter\Desktop\Python\LP3THW>
最初發表 / 最後更新: 2018.08.10 / 2018.08.10
0 comments:
張貼留言