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!
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 จะแสดงค่าต่อกันและคั่นด้วยช่องว่างอัตโนมัติ
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)
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'...') แทรกค่าตัวแปรใน {} ภายในข้อความได้โดยตรง อ่านง่ายกว่าการคั่นด้วย ,
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 ตำแหน่ง (ปัดเศษ)
โดยไม่เปลี่ยนค่าที่เก็บไว้จริง เปลี่ยนแค่การแสดงผล
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 ตำแหน่งเหมือนใบเสร็จ
Total = 750.00 baht VAT 7% = 52.50 baht
7 Common mistakes 7 ข้อผิดพลาดที่พบบ่อย
-
Missing quotes.
print(Hello)fails withNameError: without quotes Python readsHelloas a variable name. Text must be inside quotation marks. -
ลืมเครื่องหมายคำพูด
print(Hello)ทำให้เกิดNameErrorเพราะถ้าไม่มีเครื่องหมายคำพูด Python จะมองHelloเป็นชื่อตัวแปร ข้อความต้องอยู่ในเครื่องหมายคำพูดเสมอ -
Unmatched quotes or brackets.
print("Hi)is aSyntaxError: every opening quote and every(needs its closing partner. -
เครื่องหมายคำพูดหรือวงเล็บไม่ครบคู่
print("Hi)ทำให้เกิดSyntaxErrorเพราะเครื่องหมายคำพูดที่เปิดและ(ทุกตัวต้องมีตัวปิดคู่กัน -
Wrong capital letters. Python is case-sensitive, so
Print("Hi")is aNameError; the command is written in lowercase:print. -
ตัวพิมพ์เล็ก/ใหญ่ผิด Python แยกตัวพิมพ์เล็กใหญ่ ดังนั้น
Print("Hi")จะเกิดNameErrorคำสั่งต้องเขียนตัวพิมพ์เล็กprint -
Expecting
:.2fto change the value. It only changes how the number is displayed; the variable keeps all its digits, as the demo shows. -
คิดว่า
:.2fเปลี่ยนค่าจริง มันเปลี่ยนแค่การแสดงผล ตัวแปรยังเก็บทศนิยมครบเหมือนเดิม ดังตัวอย่างด้านล่าง
Number = 10.13 10.1256
8 Think further 8 แบบฝึกคิดต่อ
Try these yourself in the Run boxes above (or in Google Colab):
ลองทำเองในกล่อง Run ด้านบน (หรือใน Google Colab):
-
Print your name and age on one line with an f-string, for example
Name: Ann, age 20. -
พิมพ์ชื่อและอายุของคุณในบรรทัดเดียวด้วย f-string เช่น
Name: Ann, age 20 -
Show
3.14159rounded to 3 decimal places. Hint::.3f. -
แสดง
3.14159ให้เป็นทศนิยม 3 ตำแหน่ง Hint::.3f -
What is the difference between
print("Hello ", "World!")andprint("Hello", "World!")? Hint: count the spaces in each output. -
print("Hello ", "World!")กับprint("Hello", "World!")ต่างกันอย่างไร Hint: นับช่องว่างในผลลัพธ์ของแต่ละแบบ
Key takeaways สรุปสำคัญ
Remember these points when you write your own programs:
จำประเด็นเหล่านี้ไว้เมื่อเขียนโปรแกรมของคุณเอง:
-
print()is your window into a running program: use it to check what a variable holds and how a calculation turned out. -
print()คือหน้าต่างที่ให้เห็นการทำงานของโปรแกรม ใช้ตรวจว่าตัวแปรเก็บค่าอะไรและการคำนวณได้ผลอย่างไร - Text goes inside quotation marks; names of variables and commands do not, and Python is case-sensitive.
- ข้อความต้องอยู่ในเครื่องหมายคำพูด ส่วนชื่อตัวแปรและคำสั่งไม่ต้องใส่ และ Python แยกตัวพิมพ์เล็กใหญ่
-
An f-string with
{value:.2f}is the easiest way to mix text and numbers and to control the number of decimals. -
f-string ที่ใช้
{value:.2f}เป็นวิธีที่ง่ายที่สุดในการผสมข้อความกับตัวเลขและกำหนดจำนวนทศนิยม -
Read the error message from the bottom line up: it names the kind of error (
NameError,SyntaxError) and hints at what to fix. -
อ่านข้อความ 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:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
-
print()— Built-in Functions — print (the print command and its separator) (คำสั่ง print และตัวคั่น) -
f-strings —
Formatted string literals
(inserting values with
{}) (แทรกค่าด้วย{}) -
Format specification —
Format Specification Mini-Language
(the
:.2fdecimal format) (รูปแบบทศนิยม:.2f)