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 จริง)
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เครื่องหมายคำพูดคู่ |
\n | new line (newline)ขึ้นบรรทัดใหม่ (newline) |
\t | tabแท็บ (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 เพื่อขึ้นบรรทัดใหม่ และ \' เพื่อใส่เครื่องหมาย ' ภายในสตริงที่ใช้คำพูดเดี่ยว
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
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
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] คือตัวสุดท้าย
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 จะไปจนจบ
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 เอาทุกตัวเว้นหนึ่ง
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 — แก้ค่าในตำแหน่งเดิมไม่ได้ ต้องสร้างสตริงใหม่แทน
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) แปลงค่าเป็นสตริง
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
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() คืนสตริงใหม่
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(' ') แยกด้วยช่องว่างทีละตัว — หลายช่องว่างติดกันอาจได้ชิ้นว่าง
['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}
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:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
- Text Sequence Type — str — Built-in Types — Text Sequence Type — str (literals, operators, methods) (literals, operators, methods)
- Common string operations — string — Common string operations (related formatting) (formatting ที่เกี่ยวข้อง)
- Lexical analysis — String literals — String and Bytes literals (escape codes) (escape codes)