News Ticker

Menu

Bài 4: Print

<Python>Bài 4: Printing.


Ở bài này chúng ta sẽ tìm hiểu làm thế nào để in ra định dạng phức tạp của chuỗi.




Tạo file: Bai4.py


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

print formatter % (1 ,2, 3, 4)
print formatter % ("one" , "two" , "three" , "four")
print formatter % (True , False , False , True)
print formatter % (formatter , formatter , formatter , formatter)
print formatter % (

"I had this thing.",
"That you could type up right",
"But it didn't sing.",
"So I said goodnight"
)

Ở đoạn code trên tất cả mỗi một giá trị trong ngoặc sẽ được truyền vào một biến của "formatter".
Trong python %d được dùng với biến là số nguyên.
%s : String
%f : Float
%r : để hiển tị dạng thô nhất của dữ liệu. Nói cách khác nó để hiển thị tất cả mọi thứ

Kết quả của đoạn code:
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' 
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'


Các bạn hãy thử thay thế %r trong "formatter" bằng %d | %s để hiểu rõ thêm.

    Escape Sequences

ESCAPE
WHAT IT DOES.
\\
In ra (\)
\'
In ra  (')
\"
In ra  (")
\n
Xuống dòng (LF)
\t
Cách 1 tab (TAB)

Chúc các bạn thành công.


Share This:

Post Tags:

No Comment to " Bài 4: Print "

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM