ข้ามไปยังเนื้อหาหลัก
Editor

เมธอดของสตริงใน Python ทุกตัว อธิบายพร้อมตัวอย่าง

เริ่มต้น25 นาที6 แบบฝึกหัด100 XP

บทเรียนนี้แปลจากต้นฉบับภาษาอังกฤษRead in EnglishLeer en españolЧитать на русскомLer em portuguêsแจ้งข้อผิดพลาดในการแปล

0/6 แบบฝึกหัด

สตริง (string) ใน Python มาพร้อมเมธอด (method) ในตัวมากมาย เหมือนกล่องเครื่องมือ เมธอดเหล่านี้ช่วยให้คุณเปลี่ยนตัวพิมพ์ใหญ่-เล็ก ลบช่องว่างส่วนเกิน ค้นหาคำ แยกข้อความออกเป็นส่วน ๆ และทำอะไรได้อีกมาก

ลองนึกว่าเมธอดของสตริงเหมือนแอปในโทรศัพท์ แต่ละแอปทำงานเฉพาะอย่างเดียว ตอนนี้ยังไม่ต้องจำให้ครบทุกตัว แค่รู้ว่ามีอยู่ก็พอ เมื่อถึงเวลาต้องใช้จะได้หยิบตัวที่ใช่มาใช้ได้ทันที

ในบทเรียนนี้ คุณจะได้เรียนเมธอดของสตริงที่สำคัญทุกตัว ผ่านตัวอย่างที่รันและแก้ไขได้จริง เมื่อจบบท คุณจะทำความสะอาด ค้นหา แยก และแปลงข้อความแบบไหนก็ได้ที่ต้องเจอใน Python

วิธีแปลงสตริงเป็นตัวพิมพ์ใหญ่ใน Python

Python มีเมธอด 5 ตัวสำหรับเปลี่ยนตัวพิมพ์ใหญ่-เล็กของสตริง ตัวที่ใช้บ่อยที่สุดคือ upper() และ lower()

เมธอดเปลี่ยนตัวพิมพ์ทั้ง 5 ตัว
greeting = "hello, world!"
print(greeting.upper()) # HELLO, WORLD!
print(greeting.lower()) # hello, world!
print(greeting.title()) # Hello, World!
print(greeting.capitalize()) # Hello, world!
print(greeting.swapcase()) # HELLO, WORLD!
ผลลัพธ์
HELLO, WORLD!
hello, world!
Hello, World!
Hello, world!
HELLO, WORLD!

upper() แปลงตัวอักษรทุกตัวเป็นตัวพิมพ์ใหญ่ ส่วน lower() แปลงตัวอักษรทุกตัวเป็นตัวพิมพ์เล็ก สองตัวนี้คือเมธอดหลักที่คุณจะได้ใช้บ่อยที่สุด

title() ทำให้อักษรตัวแรกของทุกคำเป็นตัวพิมพ์ใหญ่ เหมือนชื่อหนังสือภาษาอังกฤษ ส่วน capitalize() ทำให้เฉพาะอักษรตัวแรกสุดเป็นตัวพิมพ์ใหญ่ และเปลี่ยนที่เหลือทั้งหมดเป็นตัวพิมพ์เล็ก

swapcase() สลับตัวพิมพ์ของตัวอักษรทุกตัว ตัวพิมพ์ใหญ่กลายเป็นตัวพิมพ์เล็ก และตัวพิมพ์เล็กกลายเป็นตัวพิมพ์ใหญ่ ไม่ค่อยได้ใช้ แต่สะดวกเมื่อจำเป็น

การใช้เมธอดเปลี่ยนตัวพิมพ์ในงานจริง
# Real-world use: case-insensitive comparison
user_input = "Yes"
# Without lower(), "Yes" != "yes" and your check fails
if user_input.lower() == "yes":
print("User said yes!")
# title() is great for formatting names
full_name = "ada lovelace"
print(full_name.title()) # Ada Lovelace
ผลลัพธ์
User said yes!
Ada Lovelace

วิธีลบช่องว่างออกจากสตริงใน Python

ช่องว่างส่วนเกินและตัวอักษรที่มองไม่เห็นแอบเข้ามาในสตริงอยู่บ่อย ๆ ไม่ว่าจะมาจากข้อมูลที่ผู้ใช้ป้อน การอ่านไฟล์ หรือการคัดลอกแล้ววาง เมธอดตระกูล strip() ของ Python จัดการเรื่องนี้ได้อย่างดี

strip(), lstrip() และ rstrip()
messy = " hello, world! "
print(repr(messy)) # ' hello, world! '
print(repr(messy.strip())) # 'hello, world!'
print(repr(messy.lstrip())) # 'hello, world! '
print(repr(messy.rstrip())) # ' hello, world!'
ผลลัพธ์
'   hello, world!   '
'hello, world!'
'hello, world!   '
'   hello, world!'

strip() ลบช่องว่าง (whitespace) ออกจากทั้งสองด้านของสตริง ลองนึกถึงการตัดขอบขนมปังออกทั้งสองข้างของแซนด์วิช

lstrip() ตัดเฉพาะด้านซ้าย ส่วน rstrip() ตัดเฉพาะด้านขวา ตัว "l" และ "r" ย่อมาจาก left (ซ้าย) และ right (ขวา)

ในตัวอย่างเราใช้ repr() เพื่อให้คุณมองเห็นช่องว่างได้จริง ปกติ print() จะไม่ทำให้เห็นช่องว่างที่อยู่ท้ายสตริง

ตัดตัวอักษรที่กำหนดเอง
# You can also strip specific characters
filename = "###report.txt###"
print(filename.strip("#")) # report.txt
url = "https://example.com/"
print(url.rstrip("/")) # https://example.com
ผลลัพธ์
report.txt
https://example.com

วิธีค้นหาข้อความภายในสตริงด้วย Python

Python มีหลายวิธีในการค้นหาข้อความภายในสตริง ตัวที่สำคัญที่สุดคือ find(), count(), startswith() และ endswith()

วิธีใช้ find() และ index() ใน Python

find() บอกตำแหน่งที่สตริงย่อย (substring) ปรากฏเป็นครั้งแรก โดยคืนค่าดัชนี (index) ซึ่งก็คือหมายเลขตำแหน่งของจุดแรกที่ตรงกัน เริ่มนับจาก 0

find() คืนค่าตำแหน่งของสตริงย่อย
sentence = "I love Python programming"
print(sentence.find("Python")) # 7 (starts at position 7)
print(sentence.find("love")) # 2
print(sentence.find("Java")) # -1 (not found)
print(sentence.find("o")) # 3 (first occurrence)
print(sentence.find("o", 4)) # 11 (search starting from position 4)
ผลลัพธ์
7
2
-1
3
11

เมื่อ find() หาสตริงย่อยไม่เจอ จะคืนค่า -1 นี่คือวิธีบอกว่า "ไม่พบ"

index() ทำงานเหมือน find() แต่ต่างกันในจุดสำคัญจุดเดียว: ถ้าไม่พบสตริงย่อย จะเกิดข้อผิดพลาด ValueError แทนที่จะคืนค่า -1

find(): คืนค่า -1 ถ้าไม่พบ
text = "hello world"
result = text.find("xyz")
print(result)  # -1 (safe, no crash)
index(): เกิดข้อผิดพลาดถ้าไม่พบ
text = "hello world"
# result = text.index("xyz")
# ValueError: substring not found

# Use index() only when you're SURE
# the substring exists
result = text.index("world")
print(result)  # 6

วิธีนับจำนวนครั้งที่พบในสตริงด้วย count()

count() บอกว่าสตริงย่อยปรากฏในสตริงกี่ครั้ง เมธอดนี้แยกตัวพิมพ์ใหญ่-เล็ก ดังนั้น "The" กับ "the" จึงนับแยกกัน

count() นับจำนวนครั้งที่สตริงย่อยปรากฏ
text = "the cat sat on the mat by the hat"
print(text.count("the")) # 3
print(text.count("at")) # 4 (cat, sat, mat, hat)
print(text.count("dog")) # 0
print(text.count("The")) # 0 (case-sensitive!)
ผลลัพธ์
3
4
0
0

วิธีตรวจสอบตอนต้นและตอนท้ายของสตริงใน Python

startswith() และ endswith() คืนค่า True หรือ False เหมาะมากสำหรับตรวจสอบนามสกุลไฟล์ ส่วนขึ้นต้นของ URL หรือรูปแบบใด ๆ ที่อยู่ขอบของสตริง

startswith() และ endswith()
filename = "report_2024.pdf"
print(filename.startswith("report")) # True
print(filename.endswith(".pdf")) # True
print(filename.endswith(".txt")) # False
# Check multiple options with a tuple
print(filename.endswith((".pdf", ".docx", ".txt"))) # True
ผลลัพธ์
True
True
False
True

วิธีแทนที่ข้อความในสตริงของ Python

replace() เปลี่ยนสตริงย่อยทุกตัวที่พบให้เป็นอีกสตริงหนึ่ง เหมือนฟีเจอร์ค้นหาและแทนที่ในโปรแกรมพิมพ์เอกสาร

replace() สลับสตริงย่อย
message = "I like cats. Cats are great. I have two cats."
# Replace all occurrences
print(message.replace("cats", "dogs"))
# I like dogs. Cats are great. I have two dogs.
# Notice "Cats" (capital C) was NOT replaced -- case matters!
# Limit replacements with the third argument
print(message.replace("cats", "dogs", 1))
# I like dogs. Cats are great. I have two cats.
ผลลัพธ์
I like dogs. Cats are great. I have two dogs.
I like dogs. Cats are great. I have two cats.

อาร์กิวเมนต์ตัวที่สามจะใส่หรือไม่ใส่ก็ได้ ใช้จำกัดจำนวนครั้งที่แทนที่ ถ้าไม่ใส่ replace() จะเปลี่ยนทุกตัวที่ตรงกัน

อย่าลืมว่า replace() แยกตัวพิมพ์ใหญ่-เล็ก "cats" กับ "Cats" ถือเป็นสตริงย่อยคนละตัวกัน

วิธีแยกสตริงใน Python

split() หั่นสตริงออกเป็นลิสต์ (list) ของสตริงที่สั้นลง โดยค่าเริ่มต้นจะแยกตรงช่องว่างทุกชนิด (เว้นวรรค แท็บ และการขึ้นบรรทัดใหม่)

split() แยกสตริงออกเป็นลิสต์
sentence = "Python is easy to learn"
words = sentence.split()
print(words) # ['Python', 'is', 'easy', 'to', 'learn']
# Split on a specific character
csv_line = "Alice,25,New York,Engineer"
values = csv_line.split(",")
print(values) # ['Alice', '25', 'New York', 'Engineer']
# Split with a limit
first, rest = "John Smith Jr.".split(" ", 1)
print(first) # John
print(rest) # Smith Jr.
ผลลัพธ์
['Python', 'is', 'easy', 'to', 'learn']
['Alice', '25', 'New York', 'Engineer']
John
Smith Jr.

อาร์กิวเมนต์ตัวแรกคือตัวคั่น (delimiter): ตัวอักษร (หรือสตริง) ที่ใช้เป็นจุดแยก ส่วนอาร์กิวเมนต์ตัวที่สองใช้จำกัดจำนวนครั้งที่แยก

rsplit() ทำงานแบบเดียวกัน แต่เริ่มแยกจากด้านขวา ซึ่งจะเห็นความต่างเมื่อคุณกำหนดจำนวนครั้งที่แยก

rsplit() แยกจากด้านขวา
path = "home/user/documents/report.txt"
# split from the left
print(path.split("/", 1)) # ['home', 'user/documents/report.txt']
# rsplit from the right
print(path.rsplit("/", 1)) # ['home/user/documents', 'report.txt']
ผลลัพธ์
['home', 'user/documents/report.txt']
['home/user/documents', 'report.txt']

วิธีรวมลิสต์เป็นสตริงใน Python

join() ทำงานตรงข้ามกับ split() โดยรับลิสต์ของสตริงแล้วนำมาต่อกันด้วยตัวคั่น ตัวคั่นคือสตริงที่คุณใช้เรียก join()

join() ต่อสตริงเข้าด้วยกัน
words = ["Python", "is", "awesome"]
sentence = " ".join(words)
print(sentence) # Python is awesome
# Use any separator you want
print("-".join(words)) # Python-is-awesome
print(", ".join(words)) # Python, is, awesome
print("\n".join(words)) # Each word on its own line
ผลลัพธ์
Python is awesome
Python-is-awesome
Python, is, awesome
Python
is
awesome

รูปแบบการเขียนดูกลับด้านในตอนแรก: separator.join(list) ให้คิดว่า "ใช้ตัวคั่นนี้ต่อลิสต์นั้นเข้าด้วยกัน"

รูปแบบที่พบบ่อยคือ แยกสตริง แก้ไขแต่ละส่วน แล้วต่อกลับเข้าด้วยกัน

แยก แก้ไข แล้วต่อกลับ
# Uppercase every word, then rejoin
sentence = "python is easy to learn"
words = sentence.split()
capitalized = [word.upper() for word in words]
result = " ".join(capitalized)
print(result) # PYTHON IS EASY TO LEARN
# Build a CSV line from data
fields = ["Alice", "25", "New York"]
csv_line = ",".join(fields)
print(csv_line) # Alice,25,New York
ผลลัพธ์
PYTHON IS EASY TO LEARN
Alice,25,New York

วิธีตรวจสอบเนื้อหาของสตริงใน Python

Python มีเมธอดตระกูล is____() ที่คืนค่า True หรือ False ตามสิ่งที่อยู่ในสตริง เหมาะมากสำหรับตรวจสอบความถูกต้องของข้อมูลที่ผู้ใช้ป้อน

isalpha(), isdigit(), isalnum() และ isspace()
print("hello".isalpha()) # True (only letters)
print("hello1".isalpha()) # False (contains a digit)
print("12345".isdigit()) # True (only digits)
print("12.5".isdigit()) # False (dot is not a digit)
print("hello1".isalnum()) # True (letters and digits only)
print("hello!".isalnum()) # False (! is not alphanumeric)
print(" ".isspace()) # True (only whitespace)
print(" hi ".isspace()) # False (contains letters)
ผลลัพธ์
True
False
True
False
True
False
True
False

isalpha() ตรวจว่ามีแต่ตัวอักษร isdigit() ตรวจว่ามีแต่ตัวเลข isalnum() ตรวจว่ามีแต่ตัวอักษร หรือ ตัวเลข (alphanumeric) และ isspace() ตรวจว่ามีแต่ช่องว่าง

เมธอดเหล่านี้ทุกตัวคืนค่า False เมื่อเป็นสตริงว่าง เพราะสตริงว่างไม่มีตัวอักษรให้ตรวจ จึงไม่มีอะไรผ่านการตรวจสอบ

ใช้เมธอดตรวจสอบเพื่อตรวจความถูกต้องของข้อมูล
# Practical example: validate a username
username = "player42"
if username.isalnum():
print(f"\"{username}\" is a valid username")
else:
print(f"\"{username}\" contains invalid characters")
# Check if user typed a number
user_input = "42"
if user_input.isdigit():
number = int(user_input)
print(f"You entered the number {number}")
else:
print("That's not a valid number")
ผลลัพธ์
"player42" is a valid username
You entered the number 42

วิธีเติมช่องว่างและจัดตำแหน่งสตริงใน Python

บางครั้งคุณต้องการให้ข้อความกว้างเท่าที่กำหนด: ในตาราง ใบเสร็จ หรือผลลัพธ์ในเทอร์มินัลที่ดูเป็นระเบียบ เมธอดเติมข้อความ (padding) ของ Python จะเติมตัวอักษรลงในช่องที่เหลือให้เต็ม

center(), ljust(), rjust() และ zfill()
word = "hi"
print(word.center(10)) # " hi "
print(word.center(10, "-")) # "----hi----"
print(word.ljust(10, ".")) # "hi........"
print(word.rjust(10, ".")) # "........hi"
# zfill() pads with zeros on the left
print("42".zfill(5)) # 00042
print("7".zfill(3)) # 007
ผลลัพธ์
    hi    
----hi----
hi........
........hi
00042
007

center() จัดสตริงให้อยู่ตรงกลางของความกว้างที่กำหนด ljust() จัดชิดซ้ายแล้วเติมด้านขวา และ rjust() จัดชิดขวาแล้วเติมด้านซ้าย

zfill() เป็นทางลัดสำหรับเติมเลขศูนย์ข้างหน้าตัวเลข เหมาะมากสำหรับเลขที่คำสั่งซื้อ เวลาที่บันทึกไว้ (timestamp) หรือชื่อไฟล์อย่าง image_007.png

สร้างตารางที่จัดรูปแบบแล้ว
# Build a neat table
print("Item".ljust(15) + "Price".rjust(8))
print("-" * 23)
print("Coffee".ljust(15) + "$4.50".rjust(8))
print("Sandwich".ljust(15) + "$8.99".rjust(8))
print("Cookie".ljust(15) + "$2.25".rjust(8))
print("-" * 23)
print("Total".ljust(15) + "$15.74".rjust(8))
ผลลัพธ์
Item              Price
-----------------------
Coffee            $4.50
Sandwich          $8.99
Cookie            $2.25
-----------------------
Total            $15.74

วิธีเรียกเมธอดของสตริงต่อกันใน Python

เนื่องจากเมธอดของสตริงทุกตัวคืนค่าเป็นสตริงใหม่ คุณจึงเรียกเมธอดอื่นต่อจากผลลัพธ์นั้นได้ทันที เรียกว่า การเรียกเมธอดต่อกัน (method chaining)

เรียกเมธอดต่อกัน
user_input = " Hello, World! "
# Chain multiple methods together
cleaned = user_input.strip().lower().replace(",", "")
print(cleaned) # hello world!
# Same thing step by step:
step1 = user_input.strip() # "Hello, World!"
step2 = step1.lower() # "hello, world!"
step3 = step2.replace(",", "") # "hello world!"
print(step3) # hello world!
ผลลัพธ์
hello world!
hello world!

อ่านการเรียกต่อกันจากซ้ายไปขวา: ตัดช่องว่างก่อน จากนั้นแปลงเป็นตัวพิมพ์เล็ก แล้วจึงแทนที่ แต่ละเมธอดทำงานกับผลลัพธ์ของเมธอดก่อนหน้า


แบบฝึกหัด

ได้เวลานำเมธอดของสตริงไปใช้งานจริงแล้ว แบบฝึกหัดแต่ละข้อเน้นเมธอดคนละกลุ่มที่คุณเพิ่งเรียนไป

แบบฝึกหัดที่ 1: ทำความสะอาดข้อมูลที่ผู้ใช้ป้อน
เขียนโค้ด

มีสตริงที่ไม่เรียบร้อยเก็บอยู่ในตัวแปร user_input สตริงนี้มีช่องว่างส่วนเกินทั้งสองด้าน และมีตัวพิมพ์ใหญ่ปนอยู่แบบสุ่ม

ทำความสะอาดโดย:

1. ลบช่องว่างออกจากทั้งสองด้าน

2. แปลงให้เป็นตัวพิมพ์เล็กทั้งหมด

แสดงผลลัพธ์ที่ทำความสะอาดแล้ว ผลลัพธ์ควรเป็น:

hello python
user_input = " HeLLo PyThOn "
# Clean up the input and print it
แบบฝึกหัดที่ 2: นับจำนวนครั้งที่คำปรากฏ
เขียนโค้ด

นับว่าคำ "the" ปรากฏในประโยคด้านล่างกี่ครั้ง โดยใช้เมธอด count()

แสดงผลลัพธ์ในรูปแบบนี้ให้ตรงทุกตัวอักษร:

the appears 4 times

คำใบ้: แปลงเป็นตัวพิมพ์เล็กก่อน จะได้นับทั้ง "the" และ "The"

sentence = "The cat sat on the mat by the door and The dog watched"
# Count how many times "the" appears and print it
แบบฝึกหัดที่ 3: แทนที่ทุกตัวที่พบ
เขียนโค้ด

แทนที่ "JavaScript" ทุกตัวด้วย "Python" ในประโยคด้านล่าง แล้วแสดงผลลัพธ์

ผลลัพธ์ที่ต้องการ:

I love Python. Python is great. Learning Python is fun.
sentence = "I love JavaScript. JavaScript is great. Learning JavaScript is fun."
# Replace JavaScript with Python and print it
แบบฝึกหัดที่ 4: แยกบรรทัด CSV
เขียนโค้ด

มีข้อมูลหนึ่งบรรทัดที่คั่นแต่ละค่าด้วยจุลภาค (comma) ให้แยกออกเป็นค่าแต่ละค่า แล้วแสดงแต่ละค่าในบรรทัดของตัวเอง

ผลลัพธ์ที่ต้องการ:

Alice
25
New York
Engineer
csv_line = "Alice,25,New York,Engineer"
# Split the line by commas and print each value
แบบฝึกหัดที่ 5: แก้บั๊ก find() กับ index()
แก้บั๊ก

โค้ดนี้พยายามหาตำแหน่งของ "Python" ในประโยค ทำงานได้เมื่อมีคำนั้นอยู่ แต่จะพังเมื่อไม่มี

แก้โค้ดให้:

  • แสดงตำแหน่งเมื่อพบคำ
  • แสดง "Word not found" เมื่อไม่มีคำนั้น
  • ผลลัพธ์ที่ต้องการ:

    Found at position 7
    Word not found

    คำใบ้: บั๊กอยู่ที่ index() พังเมื่อไม่มีคำนั้น เมธอดไหนที่คืนค่า -1 แทนการพัง

    sentence = "I love Python programming"
    # Find "Python" -- this line works fine
    pos = sentence.index("Python")
    print(f"Found at position {pos}")
    # Find "Java" -- this line crashes!
    pos = sentence.index("Java")
    if pos >= 0:
    print(f"Found at position {pos}")
    else:
    print("Word not found")
    แบบฝึกหัดที่ 6: ตรวจสอบชื่อผู้ใช้
    เขียนโค้ด

    เขียนโปรแกรมตรวจสอบชื่อผู้ใช้ตามกฎเหล่านี้:

  • ต้องมีแต่ตัวอักษรและตัวเลขเท่านั้น (alphanumeric)
  • ต้องยาว 3 ถึง 15 ตัวอักษร
  • มีชื่อผู้ใช้ให้ 3 ชื่อ สำหรับแต่ละชื่อ ให้แสดง "Valid" ถ้าผ่านกฎทั้งสองข้อ หรือ "Invalid" ถ้าไม่ผ่านข้อใดข้อหนึ่ง

    ผลลัพธ์ที่ต้องการ:

    player42: Valid
    ab: Invalid
    hello world!: Invalid
    usernames = ["player42", "ab", "hello world!"]
    # Check each username and print if it's valid or invalid

    สรุป: ตารางสรุปเมธอดของสตริงใน Python

    ตารางอ้างอิงแบบย่อของทุกเมธอดที่เรียนในบทนี้:

    เมธอดทำอะไรตัวอย่าง
    upper()เป็นตัวพิมพ์ใหญ่ทั้งหมด"hi".upper() → "HI"
    lower()เป็นตัวพิมพ์เล็กทั้งหมด"HI".lower() → "hi"
    title()อักษรแรกของทุกคำเป็นตัวพิมพ์ใหญ่"hi there".title() → "Hi There"
    capitalize()อักษรตัวแรกเป็นตัวพิมพ์ใหญ่"hi there".capitalize() → "Hi there"
    strip()ลบช่องว่างที่ขอบ" hi ".strip() → "hi"
    find()ตำแหน่งของสตริงย่อย (-1 ถ้าไม่พบ)"hello".find("ll") → 2
    count()นับจำนวนครั้งที่พบ"banana".count("a") → 3
    startswith()ตรวจตอนต้น"hello".startswith("he") → True
    endswith()ตรวจตอนท้าย"file.py".endswith(".py") → True
    replace()สลับสตริงย่อย"hi".replace("hi", "bye") → "bye"
    split()แยกเป็นลิสต์"a,b,c".split(",") → ["a","b","c"]
    join()ต่อลิสต์เข้าด้วยกัน",".join(["a","b"]) → "a,b"
    isdigit()มีแต่ตัวเลขหรือไม่"123".isdigit() → True
    isalpha()มีแต่ตัวอักษรหรือไม่"abc".isalpha() → True
    isalnum()มีแต่ตัวอักษรหรือตัวเลขหรือไม่"abc1".isalnum() → True
    zfill()เติมศูนย์ข้างหน้า"42".zfill(5) → "00042"

    ไม่ต้องจำตารางนี้ สิ่งสำคัญคือรู้ว่ามีเมธอดเหล่านี้อยู่ เมื่อต้องใช้จะได้เปิดหาได้

    เรียนอะไรต่อ

    ในบทถัดไป การจัดรูปแบบสตริงใน Python คุณจะได้เรียนวิธีควบคุมว่าค่าต่าง ๆ จะปรากฏในสตริงอย่างไร ทั้งการจัดรูปแบบด้วย f-string การจัดตำแหน่ง การจัดรูปแบบตัวเลข และอื่น ๆ

    บทเรียนที่เกี่ยวข้อง

    บทเรียนนี้มีประโยชน์หรือไม่