1. 程式人生 > >笨辦法學Python - 習題8-10: Printing & Printing, Printing

笨辦法學Python - 習題8-10: Printing & Printing, Printing

回車 mat 解決 閱讀 str ESS 制表符 位置 含義

目錄

  • 1、習題 8: 打印,打印
  • 2、習題 9: 打印,打印,打印
  • 3、習題 10: 那是什麽?
    • 3.1、轉義序列:
  • 4、習題總結:

1、習題 8: 打印,打印

學習目標:繼續學習 %r 的格式化輸出。

習題八中的練習代碼是:

#! -*-coding=utf-8 -*-

formatter = "%r %r %r %r %r "

print formatter % (1, "hello", [1,2,3], (1,2,3), {"name":"jack"})

print formatter % ("one", "two", "three", "four", "five")

print formatter % (True, False, True, False, False)

print formatter % (
    "I had this thing. ",
    "That you could type up right. ",
    "But it didn't sing. ",
    "So I said doognight. ",
    "Hello world."
)

上述代碼的運行結果是:

C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo8/Exer8-1.py
1 'hello' [1, 2, 3] (1, 2, 3) {'name': 'jack'} 
'one' 'two' 'three' 'four' 'five' 
True False True False False 
'I had this thing. ' 'That you could type up right. ' "But it didn't sing. " 'So I said doognight. ' 'Hello world.' 

Process finished with exit code 0

註意:上述代碼說明兩個點,一個是%r 的作用,是占位符,可以將後面給的值按原數據類型輸出(不會變),支持數字、字符串、列表、元組、字典等所有數據類型。

還有一個需要註意的就是代碼的最後一行:

print formatter % (
    "I had this thing. ",
    "That you could type up right. ",
    "But it didn't sing. ",
    "So I said doognight. ",
    "Hello world."
)
'I had this thing. ' 'That you could type up right. ' "But it didn't sing. " 'So I said doognight. ' 'Hello world.' 

最後輸出的語句中既有單引號,也有雙引號。原因在於 %r 格式化字符後是顯示字符的原始數據。而字符串的原始數據包含引號,所以我們看到其他字符串被格式化後顯示單引號。 而這條雙引號的字符串是因為原始字符串中有了單引號,為避免字符意外截斷,python 自動為這段字符串添加了雙引號。

2、習題 9: 打印,打印,打印

學習目標:了解 \n 的含義

習題九中的練習代碼是:

#! -*-coding=utf-8 -*-

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.
"""
C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo9/Exer9-1.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.

Process finished with exit code 0

上述代碼有兩個點需要註意下,一個是換行符 \n ,一個是註釋符三引號。換行符就是避免代碼過長影響閱讀性而手動進行代碼換行操作,\n 其實只是一個字符,類似的還有制表符 \t ,具體的更過的換行符知識請見下一題。

3、習題 10: 那是什麽?

學習目標:了解 \n 的含義,了解 ??的含義

首先來了解一下兩種讓字符串擴展到多行的方法:

  1. 換行符 \n (back-slash n ):兩個字符的作用是在該位置上放入一個“新行(new line)”字符
  2. 雙反斜杠(double back-slash) ??:這兩個字符組合會打印出一個反斜杠來

3.1、轉義序列:

下面介紹下再Python中常見的轉義序列:

轉義字符 描述
?(在行尾時) 續行符
? \ 反斜杠符號
單引號
" 雙引號
\a 響鈴
\b 退格(Backspace)
\e 轉義
\000
\n 換行
\v 縱向制表符
\t 橫向制表符
\r 回車
\f 換頁
\oyy 八進制數yy代表的字符,例如:\o12代表換行
\xyy 十進制數yy代表的字符,例如:\x0a代表換行
\other 其它的字符以普通格式輸出

在字符串中,有時需要包含一些特殊的符號,但是有些符號不能直接輸出,就需要使用轉義序列

舉個栗子:

在打印輸出一句話時,可能同時包含單引號和雙引號,這種情況下在print 語句中不加其他操作肯定是會出錯的。/手動滑稽

技術分享圖片

技術分享圖片

在這種情況下,我們有兩種方法解決此問題;

  1. 使用轉義序列
  2. 使用註釋符-三引號

使用轉義序列:

技術分享圖片

使用註釋符:

技術分享圖片

總結:

轉義序列就是將在print 下無法正常顯示的字符打印出來,比如說打印 ?, 換行等。

再來認識一下轉義字符 \b 的作用:作用是退格,就是刪除前一個字符的意思

[1547697550481](https://img2018.cnblogs.com/blog/1324118/201901/1324118-20190117135049172-166563149.png " \b作用 - 退格、刪除")

轉義字符 \r :也是換行作用,與 \n 不同的是光標的位置:\n 在下一行開頭,\r 在本行的開頭

print u"你好嗎?\n朋友"
print u"——分隔線——"
print u"你好嗎?\r朋友"
print "hello \rworld"
C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo10/Exer10-1.py
你好嗎?
朋友
——分隔線——
朋友
world
Process finished with exit code 0

從上面代碼可以看出來,\r 是回車,是只會打印\r 後面的內容,前面的內容自動忽略。

具體的其他制表符運用還得自己練習。

習題十中的練習代碼是:

#! -*-coding=utf-8 -*-

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

上述代碼的運行結果為:

C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo10/Exer10-1.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

Process finished with exit code 0

從上面可以看出轉義字符的含義。?t 是水平制表符, ??是用於打印 ?的。

如果將轉義字符和格式化輸出相結合,則會生成一個更復雜的格式,舉個栗子:

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

print "heloo %r " % fat_cat

print "----------------------------"

print "heloo %s " % fat_cat
C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo10/Exer10-1.py
heloo "\nI'll do a list:\n\t* Cat food\n\t* Fishies\n\t* Catnip\n\t* Grass\n" 
----------------------------
heloo 
I'll do a list:
    * Cat food
    * Fishies
    * Catnip
    * Grass

Process finished with exit code 0

從上面的代碼中可以更好的體現出格式化輸出的占位符 %r 和 %s 之間的區別。%r 是輸出原格式,%s是輸出字符串。

4、習題總結:

上面的三道習題,前兩題只是之前的知識回顧,就是格式化輸出的應用實踐,後面習題10是說明了常見轉義字符的作用,和一些續航建的轉義字符的含義。結合格式化輸出和轉義字符可以生成更復雜的格式。重點理解%s 和 %r 的作用。

笨辦法學Python - 習題8-10: Printing & Printing, Printing