Published 8月 11, 2018 by with 0 comment

習題10 - 那是什麼



用Notepad++打下列的程式碼,
另存為ex10.py.
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""

print (tabby_cat)
print (persian_cat)
print (backslash_cat)
print (fat_cat)

然後用Windows的cmd, 執行python打開它.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW

C:\Users\Peter\Desktop\Python\LP3THW>python .\ex10.py
        I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.

I'll do a list:
        * Cat food
        * Fishies
        * Catnip
        * Grass


C:\Users\Peter\Desktop\Python\LP3THW>

完成

這次練習主要是學習對字符串做更多複雜的格式化.
我們來修改一下程式碼.
Line 7, \a會讓電腦發出一嗶聲.
Line 8, 讓我知道\t會退後一個TAB(空8格).
Line 10, \b會前進一格.
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\a
123456789
\t* Cat food
 \b* Fishies
\t* Catnip\n\t* Grass
"""

print (tabby_cat)
print (persian_cat)
print (backslash_cat)
print (fat_cat)

結果,
C:\Users\Peter\Desktop\Python\LP3THW>python .\ex10.py
        I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.

I'll do a list:

123456789
        * Cat food
* Fishies
        * Catnip
        * Grass


C:\Users\Peter\Desktop\Python\LP3THW>



最初發表  /  最後更新: 2018.08.11  /  2018.08.11

0 comments:

張貼留言