Working with strings · การทำงานกับสตริง
Letters have positions
- A string is a row of characters.
- Each character has a position, called an index.
- Indexes start at
0. Uses[-1]for the last character.
ตัวอักษรมีตำแหน่ง
- String คือแถวของ ตัวอักษร
- แต่ละตัวอักษรจะมีตำแหน่ง ซึ่งเรียกว่า index
- Index เริ่มต้นที่
0ใช้s[-1]สำหรับตัวอักษรตัว สุดท้าย
word = "Python"
print(word[0])
print(word[-1])
Take a slice
- A slice takes part of a string:
s[start:end]. - The character at
endis not included. - Leave a side empty for "from the start" or "to the end":
s[:3],s[2:].
การตัดส่วน (Slice)
- Slice จะดึงบางส่วนของ string มา:
s[start:end] - ตัวอักษรที่
endจะไม่ ถูกนำมาใช้ - ทิ้งด้านใดด้านหนึ่งไว้ว่างเพื่อหมายความว่า "ตั้งแต่ต้น" หรือ "ไปจนจบ":
s[:3],s[2:]
msg = "hello"
print(msg[:3])
print(msg[2:])
Worked example: positions and slices
- In
"banana"the positions areb=0,a=1,n=2,a=3,n=4,a=5. word[0]is the first letter;word[-1]counts back from the end.word[:3]takes positions 0, 1 and 2, and stops before 3.
ตัวอย่างคำอธิบาย: ตำแหน่งและการตัดส่วน
- ใน
"banana"ตำแหน่งคือb=0,a=1,n=2,a=3,n=4,a=5 word[0]คือตัวอักษรแรก;word[-1]นับถอยหลังจากท้ายword[:3]ดึงตำแหน่ง 0, 1 และ 2 และ หยุดก่อน 3
word = "banana"
print(word[0]) # b
print(word[-1]) # a (the last letter)
print(word[:3]) # ban (positions 0, 1, 2)
print(word[2:5]) # nan (positions 2, 3, 4)
Length and changing case
len(s)gives the number of characters.s.upper()makes a CAPITAL copy;s.lower()makes a lowercase copy.- These make a new string; the original does not change.
ความยาวและการเปลี่ยนกรณี
len(s)gives the number of characters.s.upper()สร้างสำเนาแบบ ตัวพิมพ์ใหญ่;s.lower()สร้างสำเนาแบบ ตัวพิมพ์เล็ก- สิ่งเหล่านี้จะสร้าง string ใหม่; ค่าเดิมจะไม่เปลี่ยนแปลง
city = "Tokyo"
print(len(city))
print(city.upper())
print(city.lower())
f-strings build text
- An f-string starts with
fbefore the quote. - Put a value inside
{ }and Python drops it into the text. - It is the neat way to mix words and values.
f-strings สร้างข้อความ
- f-string เริ่มด้วย
fก่อนเครื่องหมายคำพูด - วางค่าภายใน
{ }แล้ว Python จะใส่ค่านั้นเข้าไปในข้อความ - เป็นวิธีที่สะอาดในการผสมคำและค่าเข้าด้วยกัน
fruit = "apple"
count = 3
print(f"I have {count} {fruit}s")
Common mistakes
- Indexing starts at
0;s[-1]is the last character. - Strings cannot be changed in place — methods like
.upper()return a NEW string, so store the result. - You can only join a string to a string with
+.
ข้อผิดพลาดที่พบบ่อย
- Indexing เริ่มต้นที่
0;s[-1]คือตัวอักษรตัวสุดท้าย - ไม่สามารถแก้ไข string เดิมได้โดยตรง — methods เช่น
.upper()จะคืนค่า string ใหม่ ดังนั้นต้องเก็บผลลัพธ์ไว้ - คุณสามารถนำ string มาต่อกับ string ได้โดยใช้
+เท่านั้น
Now you try
- Read the task to see which variable to fill or what to print.
- Press Check answer to test your code.
ลองดูเลย
- อ่านโจทย์เพื่อดูว่าต้องเติมตัวแปรตัวไหนหรือต้องพิมพ์อะไร
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Index & slice a string · การเข้าถึงตำแหน่งและการตัดส่วนของสตริง
Each character has an index from 0; a slice s[a:b] takes a range. · แต่ละตัวอักษรมี ดัชนี เริ่มตั้งแต่ 0; ส่วน ตัด s[a:b] จะดึงช่วงหนึ่งมา
word is "computer". Store its first character in first. Indexes start at 0, so that character is word[0]. · word คือ "computer" จัดเก็บตัวอักษร ตัวแรก ลงใน first ดัชนีเริ่มที่ 0 ดังนั้นตัวอักษานั้นจึงอยู่ที่ word[0]
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
first is already the first character of "computer". Store the first four characters in first_four with a slice. word[a:b] stops before b, so four characters is word[:4]. · first เป็นตัวอักษรตัวแรกของ "computer" อยู่แล้ว จัดเก็บตัวอักษร สี่ ตัวแรกลงใน first_four ด้วยการใช้การตัด word[a:b] จะหยุดก่อน b ดังนั้นสี่ตัวอักษรจึงเท่ากับ word[:4]
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
phrase is "Hello World". Store its length in n with len(...), and an UPPERCASE copy in loud with .upper(). · phrase มีค่า "Hello World". เก็บความยาวของมันลงใน n โดยใช้ len(...), และสร้างสำเนาที่เป็นอักษรพิมพ์ใหญ่ลงใน loud โดยใช้ .upper().
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
name is "Mia" and age is 12. Use an f-string to print exactly: Mia is 12 years old. · name คือ "Mia" และ age คือ 12 ใช้ f-string เพื่อพิมพ์ให้ตรงตามนี้: Mia is 12 years old.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
first is "ada" and last is "lovelace". Store the initials in initials as capital letters, each followed by a dot: "A.L.". · first คือ "ada" และ last คือ "lovelace" จัดเก็บตัวอักษรนำลงใน initials เป็นตัวพิมพ์ใหญ่ ตามด้วยจุด: "A.L."
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่