String: text, characters, and working with text String: สตริง ตัวอักษร และการจัดการข้อความ

Build strings with quotes / escape codes; operators, indexing–slicing, membership, built-in functions, methods, and formatting สร้างสตริงด้วย quotes / escape codes, ตัวดำเนินการ, indexing–slicing, membership, built-in functions, methods และ formatting

A str is a sequence of characters that cannot be changed in place (immutable). Build one with matching '...' or "..." quotes, then use escape codes, operators, index/slice, in, functions, and methods to work with the text.

str คือลำดับตัวอักษรที่เปลี่ยนไม่ได้ (immutable) สร้างด้วย '...' หรือ "..." ที่ต้องจับคู่กัน จากนั้นใช้ escape, ตัวดำเนินการ, ดัชนี/slice, in, ฟังก์ชัน และเมธอดจัดข้อความ

1 Plain string literals 1 Plain string literals

A string is made with single quotes '...' or double quotes "...". It must open and close with the same kind — a mismatch is a SyntaxError (in real Python).

สตริงสร้างด้วยเครื่องหมายคำพูดเดี่ยว '...' หรือคู่ "..." ต้องเปิด–ปิดด้วยชนิดเดียวกัน — ถ้าไม่ตรงกันจะเกิด SyntaxError (ใน Python จริง)

[1]
[output]
Single-quotes can be used
Double-quotes can be used

2 Escape codes 2 Escape codes

An escape code is a special sequence that starts with \, used for characters you cannot type directly or that need a special meaning.

Escape code คือตัวอักษรพิเศษที่ขึ้นต้นด้วย \ เพื่อแทนอักขระที่พิมพ์ตรงๆ ไม่ได้ หรือต้องการความหมายพิเศษ

Code Meaningความหมาย
\\a backslashเครื่องหมาย backslash
\'a single quoteเครื่องหมายคำพูดเดี่ยว
\"a double quoteเครื่องหมายคำพูดคู่
\nnew line (newline)ขึ้นบรรทัดใหม่ (newline)
\ttabแท็บ (tab)

2.1 Try escape codes 2.1 ทดลอง escape

Use \n for a new line and \' to put a ' inside a single-quoted string.

ใช้ \n เพื่อขึ้นบรรทัดใหม่ และ \' เพื่อใส่เครื่องหมาย ' ภายในสตริงที่ใช้คำพูดเดี่ยว

[2]
[output]
Hello
World
It's a string

3 Concatenation / repetition 3 Concatenation / repetition

With strings: + joins text (concatenation) and * repeats text (repetition) a whole number of times.

กับสตริง: + ใช้ต่อข้อความ (concatenation) และ * ใช้ทำซ้ำข้อความ (repetition) ตามจำนวนเต็ม

3.1 Try + and * 3.1 ทดลอง + และ *

'a' + 'b' gives ab; 'ha' * 3 gives hahaha.

'a' + 'b' ได้ ab ส่วน 'ha' * 3 ได้ hahaha

[3]
[output]
ab
hahaha

4 Comparing strings 4 การเปรียบเทียบสตริง

Strings compare character by character (by Unicode code) using the same operators as numbers.

สตริงเปรียบเทียบตามลำดับอักขระ (เช่น รหัส Unicode) ด้วยตัวดำเนินการเดียวกับตัวเลข

Operator Meaningความหมาย
==equal toเท่ากัน
!=not equal toไม่เท่ากัน
<less thanน้อยกว่า
<=less than or equalน้อยกว่าหรือเท่ากับ
>greater thanมากกว่า
>=greater than or equalมากกว่าหรือเท่ากับ

4.1 Try comparing 4.1 ทดลองเปรียบเทียบ

Lowercase letters usually have a higher code than uppercase, so 'a' > 'Z' is True.

ตัวอักษรพิมพ์เล็กมักมีรหัสมากกว่าพิมพ์ใหญ่ เช่น 'a' > 'Z' ได้ True

[4]
[output]
False
True
True

5 Indexing

An index starts at 0 from the left — s[i] gets the character at position i. A negative index counts from the right, e.g. s[-1] is the last character.

ดัชนี (index) เริ่มที่ 0 จากซ้าย — ใช้ s[i] ดึงตัวอักษรตำแหน่งที่ i ดัชนีติดลบนับจากขวา เช่น s[-1] คือตัวสุดท้าย

[5]
[output]
t
g

6 Slicing

A slice takes a range of text with s[start:stop] — characters from start up to just before stop. Omit start to begin at the start, omit stop to go to the end.

Slice ตัดช่วงข้อความด้วย s[start:stop] — ได้ตัวอักษรตั้งแต่ start จนถึงก่อน stop ถ้าละ start จะเริ่มต้นสตริง ถ้าละ stop จะไปจนจบ

[6]
[output]
put
Computi
puting

7 Slicing with a step 7 Slicing with step

The form s[start:stop:step] adds step to jump along, e.g. step=2 takes every other character.

รูปแบบ s[start:stop:step] เพิ่ม step เพื่อกระโดดทีละก้าว เช่น step=2 เอาทุกตัวเว้นหนึ่ง

[7]
[output]
mui
Cmui
muig

8 Membership

in / not in check whether a substring is inside a string. A string is immutable — you cannot change a character in place; you must build a new string instead.

in / not in ตรวจว่ามี substring (ข้อความย่อย) อยู่ในสตริงหรือไม่ สตริงเป็น immutable — แก้ค่าในตำแหน่งเดิมไม่ได้ ต้องสร้างสตริงใหม่แทน

[8]
[output]
True
True

9 Built-in functions 9 Built-in functions

Built-in functions often used with text: chr, ord, len, str.

ฟังก์ชันในตัวที่ใช้กับข้อความบ่อย: chr, ord, len, str

9.1 chr / len / ord / str

chr(n) turns a code into a character, ord(c) turns a character into a code, len(s) counts the length, str(x) turns a value into a string.

chr(n) แปลงรหัสเป็นตัวอักษร, ord(c) แปลงตัวอักษรเป็นรหัส, len(s) นับความยาว, str(x) แปลงค่าเป็นสตริง

[9]
[output]
A
2
65
CS24

10 Methods: search / check start and end 10 Methods: ค้นหา / ตรวจต้นท้าย

A method is called as s.method(...) — it returns a new value without changing the original string.

เมธอดเรียกด้วย s.method(...) — คืนค่าใหม่โดยไม่แก้สตริงเดิม

Method Descriptionคำอธิบาย
endswith(suffix)does it end with that text?ลงท้ายด้วยข้อความนั้นหรือไม่
startswith(prefix)does it start with that text?ขึ้นต้นด้วยข้อความนั้นหรือไม่
find(sub[, start[, end]])index of the first occurrence of a substring (-1 if not found)หาตำแหน่งแรกของ substring (ไม่พบได้ -1)

10.1 endswith / startswith / find

find returns the starting index of a substring — e.g. in 'banana', 'na' starts at position 2.

find คืนดัชนีเริ่มต้นของ substring — เช่นใน 'banana' คำว่า 'na' เริ่มที่ตำแหน่ง 2

[10]
[output]
True
True
2
4

11 Methods: character kind / letter case 11 Methods: ชนิดตัวอักษร / ตัวพิมพ์

Method Descriptionคำอธิบาย
isalnum()all letters or digitsเป็นตัวอักษรหรือตัวเลขทั้งหมด
isalpha()all lettersเป็นตัวอักษรทั้งหมด
isdigit()all digitsเป็นตัวเลขทั้งหมด
islower() / isupper()all lowercase / uppercase (at least one letter)พิมพ์เล็ก / พิมพ์ใหญ่ทั้งหมด (มีตัวอักษรอย่างน้อยหนึ่งตัว)
isspace()all whitespaceเป็นช่องว่างทั้งหมด
lower() / upper()convert to lowercase / uppercaseแปลงเป็นพิมพ์เล็ก / พิมพ์ใหญ่

11.1 Try kind and case checks 11.1 ทดลองตรวจชนิดและตัวพิมพ์

The is... methods return a boolean; lower() / upper() return a new string.

เมธอดกลุ่ม is... คืนค่าบูลีน ส่วน lower() / upper() คืนสตริงใหม่

[11]
[output]
True
True
True
True
True
HI

12 Methods: split

split() with no argument splits on any run of whitespace (several spaces together count as one). split(' ') splits on each single space — several spaces together can produce empty pieces.

split() ไม่มีอาร์กิวเมนต์จะแยกด้วยช่องว่างใดๆ (รวมหลายช่องว่างติดกัน) ส่วน split(' ') แยกด้วยช่องว่างทีละตัว — หลายช่องว่างติดกันอาจได้ชิ้นว่าง

[12]
[output]
['a', 'b', 'c']
['a', '', 'b', 'c']

13 Formatting

An f-string puts variable values in {...} inside f"...". The .format() method fills each {} in order, or by index like {0} {1}.

f-string ใส่ค่าตัวแปรใน {...} ภายใน f"..." ส่วนเมธอด .format() แทนที่ {} ตามลำดับ หรือใช้ดัชนีเช่น {0} {1}

[13]
[output]
student is Ann, year is 1.
6 5
5 6

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. Text Sequence Type — str — Built-in Types — Text Sequence Type — str (literals, operators, methods) (literals, operators, methods)
  2. Common string operations — string — Common string operations (related formatting) (formatting ที่เกี่ยวข้อง)
  3. Lexical analysis — String literals — String and Bytes literals (escape codes) (escape codes)

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

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

When making a string with quotation marks, what must you do?เมื่อสร้างสตริงด้วยเครื่องหมายคำพูด ต้องทำอย่างไร?

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

เฉลย / Answer: (A) Match '...' or "..." so the opening and closing quotes are the sameจับคู่ '...' หรือ "..." ให้เปิด–ปิดตรงกัน

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

  • (A) Match '...' or "..." so the opening and closing quotes are the sameจับคู่ '...' หรือ "..." ให้เปิด–ปิดตรงกัน
    Correct! A string must open and close with the same kind of quote.ถูกต้อง! สตริงต้องเปิดและปิดด้วยชนิดเครื่องหมายเดียวกัน
  • (B) You can always open with ' and close with "เปิดด้วย ' แล้วปิดด้วย " ได้เสมอ
    Wrong: mismatched quotes cause a SyntaxError.ผิด: เปิด–ปิดคนละชนิดจะเกิด SyntaxError
  • (C) Quotation marks are not neededไม่จำเป็นต้องมีเครื่องหมายคำพูด
    Wrong: a string must be inside quotation marks.ผิด: สตริงต้องอยู่ในเครื่องหมายคำพูด
  • (D) Only "..." is allowedใช้ได้เฉพาะ "..." เท่านั้น
    Wrong: both single and double quotes work if matched correctly.ผิด: ใช้ทั้งคำพูดเดี่ยวและคู่ได้ ถ้าจับคู่ถูกต้อง

ข้อ 2

2 / 10

What does \n mean in a string?\n ในสตริงหมายถึงอะไร?

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

เฉลย / Answer: (B) A new lineขึ้นบรรทัดใหม่

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

  • (A) A tabแท็บ (tab)
    Wrong: a tab is \t, not \n.ผิด: แท็บคือ \t ไม่ใช่ \n
  • (B) A new lineขึ้นบรรทัดใหม่
    Correct! \n is a newline.ถูกต้อง! \n คือ newline ขึ้นบรรทัดใหม่
  • (C) A single quoteเครื่องหมายคำพูดเดี่ยว
    Wrong: a single quote uses \'.ผิด: เครื่องหมายคำพูดเดี่ยวใช้ \'
  • (D) A backslashbackslash
    Wrong: a backslash uses \\.ผิด: backslash ใช้ \\

ข้อ 3

3 / 10

What is the result of 'ha' * 3?ผลลัพธ์ของ 'ha' * 3 คือข้อใด?

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

เฉลย / Answer: (C) hahahahahaha

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

  • (A) ha3ha3
    Wrong: * with a string repeats it; it does not append the 3.ผิด: * กับสตริงคือการทำซ้ำ ไม่ใช่ต่อเลข 3
  • (B) hahahaha
    Wrong: two repeats give haha, but * 3 gives three.ผิด: ทำซ้ำ 2 ครั้งจะได้ haha แต่ * 3 ได้สามรอบ
  • (C) hahahahahaha
    Correct! 'ha' repeated 3 times is hahaha.ถูกต้อง! 'ha' ซ้ำ 3 ครั้งได้ hahaha
  • (D) ha ha haha ha ha
    Wrong: no space is inserted between repeats.ผิด: ไม่มีช่องว่างแทรกระหว่างการทำซ้ำ

ข้อ 4

4 / 10

What is the result of 'a' > 'Z'?ผลลัพธ์ของ 'a' > 'Z' คือข้อใด?

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

เฉลย / Answer: (D) TrueTrue

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

  • (A) FalseFalse
    Wrong: lowercase letters usually have a higher code than uppercase.ผิด: ตัวพิมพ์เล็กมักมีรหัสมากกว่าพิมพ์ใหญ่
  • (B) ErrorError
    Wrong: strings can be compared with >.ผิด: เปรียบเทียบสตริงด้วย > ได้
  • (C) NoneNone
    Wrong: a comparison returns True or False.ผิด: การเปรียบเทียบคืน True หรือ False
  • (D) TrueTrue
    Correct! 'a' has a higher code than 'Z', so the result is True.ถูกต้อง! 'a' มีรหัสมากกว่า 'Z' จึงได้ True

ข้อ 5

5 / 10

What is the result of "Computing"[5]?ผลลัพธ์ของ "Computing"[5] คือข้อใด?

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

เฉลย / Answer: (A) tt

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

  • (A) tt
    Correct! Index 0=C, 1=o, 2=m, 3=p, 4=u, 5=t.ถูกต้อง! ดัชนี 0=C,1=o,2=m,3=p,4=u,5=t
  • (B) uu
    Wrong: u is at index 4.ผิด: u อยู่ที่ดัชนี 4
  • (C) ii
    Wrong: i is at index 6.ผิด: i อยู่ที่ดัชนี 6
  • (D) CC
    Wrong: C is at index 0.ผิด: C อยู่ที่ดัชนี 0

ข้อ 6

6 / 10

What is the result of "Computing"[3:6]?ผลลัพธ์ของ "Computing"[3:6] คือข้อใด?

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

เฉลย / Answer: (B) putput

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

  • (A) Comp Comp
    Wrong: the slice has no leading space and starts at index 3.ผิด: slice ไม่รวมช่องว่างนำหน้า และเริ่มที่ดัชนี 3
  • (B) putput
    Correct! Indexes 3, 4, 5 are p, u, t (6 is not included).ถูกต้อง! ดัชนี 3,4,5 คือ p,u,t (ไม่รวม 6)
  • (C) putiputi
    Wrong: including index 6 would give puti, but stop is not included.ผิด: รวมถึงดัชนี 6 จะได้ puti แต่ stop ไม่ถูกรวม
  • (D) utinguting
    Wrong: that is closer to [4:], not [3:6].ผิด: นั่นใกล้เคียงกับ [4:] ไม่ใช่ [3:6]

ข้อ 7

7 / 10

What is the result of '2' in 'AB12'?ผลลัพธ์ของ '2' in 'AB12' คือข้อใด?

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

เฉลย / Answer: (C) TrueTrue

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

  • (A) FalseFalse
    Wrong: the character 2 is inside AB12.ผิด: มีตัวอักษร 2 อยู่ใน AB12
  • (B) 22
    Wrong: in returns a boolean, not a character.ผิด: in คืนค่าบูลีน ไม่ใช่ตัวอักษร
  • (C) TrueTrue
    Correct! '2' is a substring of 'AB12'.ถูกต้อง! '2' เป็น substring ของ 'AB12'
  • (D) NoneNone
    Wrong: membership returns True or False.ผิด: membership คืน True หรือ False

ข้อ 8

8 / 10

What is the result of ord('A')?ผลลัพธ์ของ ord('A') คือข้อใด?

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

เฉลย / Answer: (D) 6565

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

  • (A) 'A''A'
    Wrong: ord returns a numeric code, not a character.ผิด: ord คืนรหัสตัวเลข ไม่ใช่ตัวอักษร
  • (B) 9797
    Wrong: 97 is the code for 'a', not 'A'.ผิด: 97 คือรหัสของ 'a' ไม่ใช่ 'A'
  • (C) chr(65)chr(65)
    Wrong: that is the reverse function, not the result of ord.ผิด: นั่นคือฟังก์ชันกลับทาง ไม่ใช่ผลของ ord
  • (D) 6565
    Correct! ord('A') gives the Unicode/ASCII code 65.ถูกต้อง! ord('A') ได้รหัส Unicode/ASCII เป็น 65

ข้อ 9

9 / 10

What is the result of 'banana'.find('na')?ผลลัพธ์ของ 'banana'.find('na') คือข้อใด?

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

เฉลย / Answer: (A) 22

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

  • (A) 22
    Correct! 'na' starts at index 2 in banana.ถูกต้อง! 'na' เริ่มที่ดัชนี 2 ใน banana
  • (B) 33
    Wrong: index 3 is the second a, not where the first 'na' starts.ผิด: ดัชนี 3 คือตัว a ตัวที่สอง ไม่ใช่จุดเริ่มของ na แรก
  • (C) TrueTrue
    Wrong: find returns an integer position, not a boolean.ผิด: find คืนตำแหน่งเป็นจำนวนเต็ม ไม่ใช่บูลีน
  • (D) -1-1
    Wrong: -1 means not found, but here it is found.ผิด: -1 หมายถึงไม่พบ แต่ในที่นี้พบ

ข้อ 10

10 / 10

What does print('{1} {0}'.format(6, 5)) show?ผลลัพธ์ของ print('{1} {0}'.format(6, 5)) คือข้อใด?

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

เฉลย / Answer: (B) 5 65 6

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

  • (A) 6 56 5
    Wrong: {0} is the first argument (6), but the text lists {1} first.ผิด: {0} คืออาร์กิวเมนต์ตัวแรก (6) แต่ลำดับในข้อความคือ {1} ก่อน
  • (B) 5 65 6
    Correct! {1} is 5 and {0} is 6, so the result is 5 6.ถูกต้อง! {1} คือ 5 และ {0} คือ 6 จึงได้ 5 6
  • (C) {1} {0}{1} {0}
    Wrong: format replaces the braces with the real values.ผิด: format จะแทนที่วงเล็บปีกกาด้วยค่าจริง
  • (D) 1 01 0
    Wrong: the numbers in braces are argument indexes, not the printed values.ผิด: ตัวเลขในวงเล็บปีกกาคือดัชนีอาร์กิวเมนต์ ไม่ใช่ค่าที่พิมพ์