Getting started with Python: using print Python Getting Started: การใช้คำสั่ง print

Start showing text and variable values with print() เริ่มต้นแสดงข้อความและค่าตัวแปรด้วย print()

print() is the command that shows text on the screen. You use it to check how a program runs, to check that a variable holds the value you expect, and to communicate with the user. It can be used in several ways, shown below. Each example has an editable code box and a Run button — the expected output is already shown so you can compare.

print() คือคำสั่งแสดงข้อความ ใช้สำหรับตรวจสอบการทำงานของโปรแกรม ตรวจสอบตัวแปรว่าให้ผลลัพธ์ตรงที่ต้องการหรือไม่ และเป็นข้อความสำหรับสื่อสารกับผู้ใช้งาน รูปแบบการใช้คำสั่งนี้มีหลายรูปแบบ ดังนี้ แต่ละตัวอย่างมีกล่องโค้ดที่แก้ไขได้และปุ่ม Run โดยแสดงผลลัพธ์ที่ควรได้ไว้ให้เทียบแล้ว

What is Python? Python คืออะไร

Python is a general-purpose programming language known for code that is easy to read. It is used to build websites, analyse data, automate repetitive tasks, control instruments, and run scientific and engineering calculations. A program is plain text with one instruction per line, and Python runs it from top to bottom. In this lesson you learn the first tool every Python programmer uses — print() — to make a program show its results.

Python เป็นภาษาโปรแกรมอเนกประสงค์ที่เขียนแล้วอ่านง่าย ใช้สร้างเว็บไซต์ วิเคราะห์ข้อมูล ทำงานซ้ำ ๆ ให้อัตโนมัติ ควบคุมเครื่องมือวัด และคำนวณทางวิทยาศาสตร์หรือวิศวกรรม โปรแกรมคือข้อความธรรมดาที่เขียนหนึ่งคำสั่งต่อหนึ่งบรรทัด และ Python จะรันจากบนลงล่าง ในบทนี้คุณจะได้เรียนเครื่องมือแรกที่ผู้เขียนโปรแกรม Python ทุกคนใช้ คือ print() เพื่อให้โปรแกรมแสดงผลลัพธ์

1 Showing a simple message 1 การแสดงข้อความอย่างง่าย

Use print() with text inside quotation marks "..." to show a message on screen — the classic first example is Hello, World!.

ใช้ print() พร้อมข้อความในเครื่องหมายคำพูด "..." เพื่อแสดงข้อความบนหน้าจอ — ตัวอย่างมาตรฐานเริ่มต้น Hello, World!

[1]
[output]
Hello, World!

2 Joining text with several arguments 2 การต่อข้อความด้วยหลายอาร์กิวเมนต์

Pass several values to print() separated by , — Python shows them one after another and automatically puts a space between each.

ส่งหลายค่าเข้า print() คั่นด้วย , — Python จะแสดงค่าต่อกันและคั่นด้วยช่องว่างอัตโนมัติ

[2]
[output]
Hello  World!

3 Showing a variable's value 3 การแสดงค่าตัวแปร

Set a variable, then use print() to show a label together with the variable's value, separated by , — e.g. print("Number =", Number).

กำหนดตัวแปรแล้วใช้ print() แสดงข้อความร่วมกับค่าตัวแปร โดยคั่นด้วย , เช่น print("Number =", Number)

[3]
[output]
Number = 10

4 Formatting text with an f-string 4 การจัดรูปแบบข้อความด้วย f-string

An f-string (f'...') lets you drop a variable's value straight into the text inside {}. It is easier to read than separating with ,.

ใช้ f-string (f'...') แทรกค่าตัวแปรใน {} ภายในข้อความได้โดยตรง อ่านง่ายกว่าการคั่นด้วย ,

[4]
[output]
Number = 10

5 Choosing how many decimal places to show 5 การกำหนดจำนวนทศนิยมที่แสดง

Inside an f-string, the form {variable:.2f} shows the number rounded to 2 decimal places. This does not change the stored value — only how it is displayed.

ใน f-string ใช้รูปแบบ {ตัวแปร:.2f} เพื่อแสดงตัวเลขเป็นทศนิยม 2 ตำแหน่ง (ปัดเศษ) โดยไม่เปลี่ยนค่าที่เก็บไว้จริง เปลี่ยนแค่การแสดงผล

[5]
[output]
Number = 10.13

6 A shop receipt 6 ใบเสร็จร้านค้า

Real programs mix variables, arithmetic and formatted output. The operator * means multiplication. Here the total and the 7% VAT are calculated first, then printed with two decimal places, the way a receipt shows money.

โปรแกรมจริงมักผสมตัวแปร การคำนวณ และการจัดรูปแบบผลลัพธ์ เครื่องหมาย * คือการคูณ ตัวอย่างนี้คำนวณยอดรวมและ VAT 7% ก่อน แล้วพิมพ์ทศนิยม 2 ตำแหน่งเหมือนใบเสร็จ

[6]
[output]
Total = 750.00 baht
VAT 7% = 52.50 baht

7 Common mistakes 7 ข้อผิดพลาดที่พบบ่อย

  1. Missing quotes. print(Hello) fails with NameError: without quotes Python reads Hello as a variable name. Text must be inside quotation marks.
  2. ลืมเครื่องหมายคำพูด print(Hello) ทำให้เกิด NameError เพราะถ้าไม่มีเครื่องหมายคำพูด Python จะมอง Hello เป็นชื่อตัวแปร ข้อความต้องอยู่ในเครื่องหมายคำพูดเสมอ
  3. Unmatched quotes or brackets. print("Hi) is a SyntaxError: every opening quote and every ( needs its closing partner.
  4. เครื่องหมายคำพูดหรือวงเล็บไม่ครบคู่ print("Hi) ทำให้เกิด SyntaxError เพราะเครื่องหมายคำพูดที่เปิดและ ( ทุกตัวต้องมีตัวปิดคู่กัน
  5. Wrong capital letters. Python is case-sensitive, so Print("Hi") is a NameError; the command is written in lowercase: print.
  6. ตัวพิมพ์เล็ก/ใหญ่ผิด Python แยกตัวพิมพ์เล็กใหญ่ ดังนั้น Print("Hi") จะเกิด NameError คำสั่งต้องเขียนตัวพิมพ์เล็ก print
  7. Expecting :.2f to change the value. It only changes how the number is displayed; the variable keeps all its digits, as the demo shows.
  8. คิดว่า :.2f เปลี่ยนค่าจริง มันเปลี่ยนแค่การแสดงผล ตัวแปรยังเก็บทศนิยมครบเหมือนเดิม ดังตัวอย่างด้านล่าง
[7]
[output]
Number = 10.13
10.1256

8 Think further 8 แบบฝึกคิดต่อ

Try these yourself in the Run boxes above (or in Google Colab):

ลองทำเองในกล่อง Run ด้านบน (หรือใน Google Colab):

  1. Print your name and age on one line with an f-string, for example Name: Ann, age 20.
  2. พิมพ์ชื่อและอายุของคุณในบรรทัดเดียวด้วย f-string เช่น Name: Ann, age 20
  3. Show 3.14159 rounded to 3 decimal places. Hint: :.3f.
  4. แสดง 3.14159 ให้เป็นทศนิยม 3 ตำแหน่ง Hint: :.3f
  5. What is the difference between print("Hello ", "World!") and print("Hello", "World!")? Hint: count the spaces in each output.
  6. print("Hello ", "World!") กับ print("Hello", "World!") ต่างกันอย่างไร Hint: นับช่องว่างในผลลัพธ์ของแต่ละแบบ

Key takeaways สรุปสำคัญ

Remember these points when you write your own programs:

จำประเด็นเหล่านี้ไว้เมื่อเขียนโปรแกรมของคุณเอง:

  1. print() is your window into a running program: use it to check what a variable holds and how a calculation turned out.
  2. print() คือหน้าต่างที่ให้เห็นการทำงานของโปรแกรม ใช้ตรวจว่าตัวแปรเก็บค่าอะไรและการคำนวณได้ผลอย่างไร
  3. Text goes inside quotation marks; names of variables and commands do not, and Python is case-sensitive.
  4. ข้อความต้องอยู่ในเครื่องหมายคำพูด ส่วนชื่อตัวแปรและคำสั่งไม่ต้องใส่ และ Python แยกตัวพิมพ์เล็กใหญ่
  5. An f-string with {value:.2f} is the easiest way to mix text and numbers and to control the number of decimals.
  6. f-string ที่ใช้ {value:.2f} เป็นวิธีที่ง่ายที่สุดในการผสมข้อความกับตัวเลขและกำหนดจำนวนทศนิยม
  7. Read the error message from the bottom line up: it names the kind of error (NameError, SyntaxError) and hints at what to fix.
  8. อ่านข้อความ error จากบรรทัดล่างสุด เพราะบอกชนิดของ error (NameError, SyntaxError) และให้เบาะแสว่าต้องแก้อะไร

Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab

References References / แหล่งอ้างอิง

Sources used for this lesson:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. print() — Built-in Functions — print (the print command and its separator) (คำสั่ง print และตัวคั่น)
  2. f-strings — Formatted string literals (inserting values with {}) (แทรกค่าด้วย {})
  3. Format specification — Format Specification Mini-Language (the :.2f decimal format) (รูปแบบทศนิยม :.2f)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

What is print() for?print() ใช้ทำอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) Showing text or a value on screenแสดงข้อความหรือค่าบนหน้าจอ

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Showing text or a value on screenแสดงข้อความหรือค่าบนหน้าจอ
    Correct! print() shows text, checks variable values, and communicates with the user.ถูกต้อง! print() ใช้แสดงข้อความ ตรวจสอบค่าตัวแปร และสื่อสารกับผู้ใช้
  • (B) Creating a new program fileสร้างไฟล์โปรแกรมใหม่
    Wrong: print() does not create a file; it shows output.ผิด: print() ไม่ได้สร้างไฟล์ แต่ใช้แสดงผล
  • (C) Deleting a variable from memoryลบตัวแปรออกจากหน่วยความจำ
    Wrong: print() does not delete variables.ผิด: print() ไม่ได้ลบตัวแปร
  • (D) Repeating a statement several timesวนซ้ำคำสั่งหลายรอบ
    Wrong: looping is not print()'s job.ผิด: การวนซ้ำไม่ใช่หน้าที่ของ print()

ข้อ 2

2 / 10

What does print("Hello, World!") show?print("Hello, World!") แสดงอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Hello, World!Hello, World!

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Hello World (no comma)Hello World (ไม่มีเครื่องหมายจุลภาค)
    Wrong: text in quotes is shown exactly as written, comma included.ผิด: ข้อความในเครื่องหมายคำพูดจะแสดงตรงตามที่เขียน รวมจุลภาค
  • (B) Hello, World!Hello, World!
    Correct! print shows the text in the quotes literally.ถูกต้อง! print แสดงข้อความในเครื่องหมายคำพูดตามตัวอักษร
  • (C) "Hello, World!" (with the quotes)"Hello, World!" (รวมเครื่องหมายคำพูด)
    Wrong: the quotes wrap the text and are not printed.ผิด: เครื่องหมายคำพูดใช้ครอบข้อความ ไม่ถูกแสดงออกมา
  • (D) ErrorError
    Wrong: this statement is valid and runs.ผิด: คำสั่งนี้ถูกต้องและรันได้

ข้อ 3

3 / 10

When you pass several values to print separated by ,, what does Python put between them?ส่งหลายค่าเข้า print คั่นด้วย , แล้ว Python คั่นผลลัพธ์ด้วยอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) A spaceช่องว่าง

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Nothing — they run togetherไม่มีอะไร — ติดกันเลย
    Wrong: Python automatically separates them with a space.ผิด: Python จะคั่นค่าด้วยช่องว่างอัตโนมัติ
  • (B) A comma ,เครื่องหมายจุลภาค ,
    Wrong: the comma in code separates arguments; it is not printed.ผิด: จุลภาคในโค้ดใช้แยกอาร์กิวเมนต์ ไม่ได้พิมพ์ออกมา
  • (C) A spaceช่องว่าง
    Correct! With comma-separated values, Python separates the output with a space.ถูกต้อง! เมื่อส่งหลายค่าคั่นด้วย , Python จะคั่นผลลัพธ์ด้วยช่องว่าง
  • (D) A new line between every valueขึ้นบรรทัดใหม่ระหว่างทุกค่า
    Wrong: values in one print stay on one line, separated by a space.ผิด: ค่าใน print เดียวกันอยู่บรรทัดเดียว คั่นด้วยช่องว่าง

ข้อ 4

4 / 10

Which form inserts a variable into text with an f-string?วิธีแทรกตัวแปรในข้อความด้วย f-string ใช้รูปแบบใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) print(f'Number = {Number}')print(f'Number = {Number}')

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) print('Number = Number')print('Number = Number')
    Wrong: this shows the word Number as text, not the variable's value.ผิด: แบบนี้แสดงคำว่า Number เป็นข้อความ ไม่ได้อ่านค่าตัวแปร
  • (B) Only print("Number =" + Number) worksprint("Number =" + Number) เท่านั้นที่ใช้ได้
    Wrong: this lesson teaches f-strings; it does not say + is the only way.ผิด: บทนี้สอน f-string ไม่ได้บอกว่าต้องใช้ + เท่านั้น
  • (C) print(Number f'...')print(Number f'...')
    Wrong: the f must come right before the opening quote.ผิด: ต้องขึ้นต้นด้วย f ก่อนเครื่องหมายคำพูด
  • (D) print(f'Number = {Number}')print(f'Number = {Number}')
    Correct! Use f'...' and put the variable name in {}, as in the lesson.ถูกต้อง! ใช้ f'...' และใส่ชื่อตัวแปรใน {} ตามตัวอย่างในบท

ข้อ 5

5 / 10

In an f-string, what does {Number:.2f} do?ใน f-string รูปแบบ {Number:.2f} ทำอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) Shows the number to 2 decimal placesแสดงทศนิยม 2 ตำแหน่ง

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Shows the number to 2 decimal placesแสดงทศนิยม 2 ตำแหน่ง
    Correct! :.2f displays the value rounded to 2 decimal places.ถูกต้อง! :.2f จำกัดการแสดงผลเป็นทศนิยม 2 ตำแหน่ง
  • (B) Multiplies the value by 2คูณค่าด้วย 2
    Wrong: .2f does not multiply; it formats the decimal.ผิด: .2f ไม่ได้คูณค่า แต่จัดรูปแบบทศนิยม
  • (C) Converts to a 2-digit whole numberแปลงเป็นจำนวนเต็ม 2 หลัก
    Wrong: .2f is about decimals, not a 2-digit integer.ผิด: .2f เกี่ยวกับทศนิยม ไม่ใช่จำนวนเต็ม 2 หลัก
  • (D) Removes all decimalsลบทศนิยมทั้งหมดออก
    Wrong: removing all decimals is not what .2f means.ผิด: การลบทศนิยมทั้งหมดไม่ใช่ความหมายของ .2f

ข้อ 6

6 / 10

What are the quotation marks "..." around text in print for?เครื่องหมายคำพูด "..." รอบข้อความใน print มีไว้ทำอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Marking the text inside as a string (literal text) to show as-isบอกว่าข้อความข้างในเป็นสตริง (ตัวอักษร) ที่จะแสดงตรงๆ

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Telling Python to run the text as a commandบอก Python ว่าให้รันข้อความนั้นเป็นคำสั่ง
    Wrong: quoted text is literal data, not a command.ผิด: ข้อความในเครื่องหมายคำพูดคือข้อมูลตัวอักษร ไม่ใช่คำสั่ง
  • (B) Marking the text inside as a string (literal text) to show as-isบอกว่าข้อความข้างในเป็นสตริง (ตัวอักษร) ที่จะแสดงตรงๆ
    Correct! Quotation marks mark the start and end of a string.ถูกต้อง! เครื่องหมายคำพูดกำหนดขอบเขตของสตริง
  • (C) Making the text boldทำให้ข้อความเป็นตัวหนา
    Wrong: quotes have nothing to do with text styling.ผิด: เครื่องหมายคำพูดไม่เกี่ยวกับรูปแบบตัวอักษร
  • (D) Needed only for numbersจำเป็นเฉพาะกับตัวเลขเท่านั้น
    Wrong: numbers need no quotes, but text does.ผิด: ตัวเลขไม่ต้องใส่เครื่องหมายคำพูด แต่ข้อความต้องใส่

ข้อ 7

7 / 10

What does print("a", "b") show?print("a", "b") แสดงอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) a ba b

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) abab
    Wrong: comma-separated values get a space between them.ผิด: ค่าที่คั่นด้วย , จะมีช่องว่างคั่น
  • (B) a,ba,b
    Wrong: the comma is not printed.ผิด: จุลภาคไม่ถูกพิมพ์ออกมา
  • (C) a ba b
    Correct! Python separates them with one space.ถูกต้อง! Python คั่นด้วยช่องว่างหนึ่งช่อง
  • (D) a and b on separate linesa และ b คนละบรรทัด
    Wrong: they are on the same line.ผิด: อยู่บรรทัดเดียวกัน

ข้อ 8

8 / 10

How must an f-string be written to be correct?f-string ต้องเขียนอย่างไรจึงถูกต้อง?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) Put f before the opening quote, then variable names in {}ใส่ f ก่อนเครื่องหมายคำพูดเปิด แล้วใส่ชื่อตัวแปรใน {}

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Put f after the quotes, e.g. '...'fใส่ f ไว้หลังเครื่องหมายคำพูด เช่น '...'f
    Wrong: f must come before the opening quote.ผิด: f ต้องอยู่ก่อนเครื่องหมายคำพูดเปิด
  • (B) Use empty {} with no variable nameใช้ {} ว่างๆ โดยไม่ต้องมีชื่อตัวแปร
    Wrong: you must put a name or expression inside {}.ผิด: ต้องใส่ชื่อตัวแปรหรือนิพจน์ใน {}
  • (C) No quotes at allไม่ต้องมีเครื่องหมายคำพูดเลย
    Wrong: an f-string is still a string and needs quotes.ผิด: f-string ยังเป็นสตริง ต้องมีเครื่องหมายคำพูด
  • (D) Put f before the opening quote, then variable names in {}ใส่ f ก่อนเครื่องหมายคำพูดเปิด แล้วใส่ชื่อตัวแปรใน {}
    Correct! For example, f'x = {x}'.ถูกต้อง! เช่น f'x = {x}'

ข้อ 9

9 / 10

After Number = 10.1256 and print(f'{Number:.2f}'), does the stored value of Number change?หลัง Number = 10.1256 แล้ว print(f'{Number:.2f}') ค่า Number ที่เก็บไว้เปลี่ยนไปไหม?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) No, it stays 10.1256 — :.2f only changes the displayไม่เปลี่ยน ยังเป็น 10.1256 — :.2f เปลี่ยนแค่การแสดงผล

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) It becomes 10.13เปลี่ยนไปเป็น 10.13
    Wrong: :.2f changes only the display, not the stored value.ผิด: :.2f เปลี่ยนแค่การแสดงผล ไม่แตะค่าที่เก็บ
  • (B) No, it stays 10.1256 — :.2f only changes the displayไม่เปลี่ยน ยังเป็น 10.1256 — :.2f เปลี่ยนแค่การแสดงผล
    Correct! Formatting in an f-string does not modify the variable.ถูกต้อง! การจัดรูปแบบใน f-string ไม่แก้ค่าตัวแปร
  • (C) It becomes the string "10.13"เปลี่ยนเป็นสตริง "10.13"
    Wrong: Number is still the same float.ผิด: Number ยังเป็น float เดิม
  • (D) It becomes 0 after printingกลายเป็น 0 หลังพิมพ์
    Wrong: print does not clear a variable.ผิด: print ไม่ล้างค่าตัวแปร

ข้อ 10

10 / 10

Which line shows a label together with a variable's value on one line (without an f-string)?บรรทัดใดแสดงป้ายกำกับพร้อมค่าตัวแปรในบรรทัดเดียว (ไม่ใช้ f-string)?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) print("Number =", Number)print("Number =", Number)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) print("Number = Number")print("Number = Number")
    Wrong: the second Number is inside quotes, so it is text.ผิด: Number ตัวที่สองอยู่ในเครื่องหมายคำพูด จึงเป็นข้อความ
  • (B) print Numberprint Number
    Wrong: calling print needs brackets.ผิด: เรียก print ต้องมีวงเล็บ
  • (C) print("Number =", Number)print("Number =", Number)
    Correct! Separate the label text and the variable with a comma, as in the lesson.ถูกต้อง! คั่นป้ายข้อความกับตัวแปรด้วย , ตามตัวอย่างในบท
  • (D) print("Number =" Number)print("Number =" Number)
    Wrong: it is missing the comma between the arguments.ผิด: ขาดเครื่องหมายจุลภาคระหว่างอาร์กิวเมนต์