ข้อความหรือ String คือ ข้อมูลที่เป็นตัวอักษร ข้อความ หรือ ประโยค เป็นข้อมูลที่ไม่สามารถนำมาคำนวณได้โดยตรง ในการประกาศตัวแปรชนิดนี้จะต้องอยู่ภายในเครื่องหมาย Single quote ( ‘) หรือ Double quote (”) อย่างใดอย่างหนึ่งเสมอ
a = "Hello" b = 'Hello'
“Hello” มีค่าเท่ากับ ‘Hello’
หากเราต้องการให้ เครื่องหมาย Single quote ( ‘) หรือ Double quote (”) อยู่ในประโยค สามารถใช้ \ นำหน้า เช่น
a = "I\'m a programmer." b = "He said \"Hello Python\""
ภาษา python สามารถประกาศตัวแปร String โดยกำหนดข้อมูลหลายบรรทัดได้ โดยใช้เครื่องหมาย quote 3 ตัว เช่น
str = """Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable""" str2 = '''Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable''' print(str) print() print(str2)
ผลลัพท์
Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable Process finished with exit code 0
String คืออาร์เรย์
เช่นเดียวกับในหลายๆภาษาคือ String คือ อาร์เรย์ชนิดหนึ่ง แต่มีความแตกต่างคือ ในภาษาอื่น String จะเป็นอาร์เรย์ของข้อมูลชนิด character แต่ในภาษา python Stringจะเป็นอาร์เรย์ของข้อมูล String หรือ String ประกอบขึ้นจาก String ย่อย 1 ตัวมาประกอบกัน
** ภาษา python ไม่มีชนิดข้อมูล character
Coding
str = "Hello World" print("str[0]:",str[0],"type of str[0]:",type(str[0])) print("str[1]:",str[1],"type of str[1]:",type(str[1])) print("str[2]:",str[2],"type of str[2]:",type(str[2])) print("str[3]:",str[3],"type of str[3]:",type(str[3])) print("str[4]:",str[4],"type of str[4]:",type(str[4])) print("str[5]:",str[5],"type of str[5]:",type(str[5])) print("str[6]:",str[6],"type of str[6]:",type(str[6])) print("str[7]:",str[7],"type of str[7]:",type(str[7])) print("str[8]:",str[8],"type of str[8]:",type(str[8])) print("str[9]:",str[9],"type of str[9]:",type(str[9])) print("str[10]:",str[10],"type of str[10]:",type(str[10]))
ผลลัพท์
str[0]: H type of str[0]: <class 'str'> str[1]: e type of str[1]: <class 'str'> str[2]: l type of str[2]: <class 'str'> str[3]: l type of str[3]: <class 'str'> str[4]: o type of str[4]: <class 'str'> str[5]: type of str[5]: <class 'str'> str[6]: W type of str[6]: <class 'str'> str[7]: o type of str[7]: <class 'str'> str[8]: r type of str[8]: <class 'str'> str[9]: l type of str[9]: <class 'str'> str[10]: d type of str[10]: <class 'str'>
จากตัวอย่างจะพบว่า ข้อมูล String ประกอบจากข้อมูล String ย่อยมาต่อกัน
การเข้าถึงตัวอักษรตำแหน่งต่างๆ ของ String ได้ผ่านทาง Index ของมัน โดยระบุเลข index ภายในครื่องหมาย [] เช่น str = “Hello World”

ถ้าต้องการเข้าถึง ‘W’ ในตัวแปร str ให้อ้าง str[6]
ถ้าต้องการเข้าถึง ‘o’ ในตัวแปร str ให้อ้าง str[4] และ str[7]
จะเห็นว่า Index จะเริ่มนับจาก 0 แล้วเพิ่มขึ้นเป็น 1 , 2 , …. , N-1 เมื่อ N คือจำนวนอักษรในตัวแปร String
ถ้าต้องการเข้าถึงช่วงของตัวอักษร เช่น
ถ้าต้องการเข้าถึง ‘Hello’ ในตัวแปร str ให้อ้าง str[0:5] คือการระบุข้อมูล String ตั้งแต่ตำแหน่งที่ 0 จนถึงตำแหน่งที่ 4 (ไม่เอาตำแหน่งที่ 5)
ถ้าต้องการเข้าถึง ‘World’ ในตัวแปร str ให้อ้าง str[6:11] คือการระบุข้อมูล String ตั้งแต่ตำแหน่งที่ 6 จนถึงตำแหน่งที่ 10 (ไม่เอาตำแหน่งที่ 11)
Coding
str = "Hello World" print("str[0:5]:",str[0:5]) print("str[6:11]:",str[6:11])
ผลลัพท์
str[0:5]: Hello str[6:11]: World
ความยาวของข้อมูล String
สามารถทราบความยาว หรือจำนวนอักษรในตัวแปร String โดยใช้ฟังก์ชัน len()
Coding
str = "Hello World" print("length of a string:",len(str))
ผลลัพท์
length of a string: 11
จากตัวอย่าง ตัวแปร str ประกอบจากอักษร 11 ตัว หรือมีความยาว 11 โดยมี index เริ่มต้นจาก 0 , 1 ,2 ,…,10
การใช้งาน String methods
ในการเขียนโปรแกรม เราคงเคยได้ยินคำว่า ฟังก์ชัน กับคำว่า เมธอด หลายๆคนอาจจะสงสัยว่า เมธอด ต่างจาก ฟังก์ชันอย่างไร?
เมธอด คือฟังก์ชันการทำงานที่กำหนดไว้ในคลาส หรือ เมธอดคือฟังก์ชันของคลาสนั่นเอง
เนื่องจากข้อมูล String ในภาษา python คือ คลาสชนิดหนึ่ง ดังนั้น String จึงประกอบด้วยเมธอดจำนวนมาก ในบทนี้เราจะเรียนรู้เมธอดที่น่าสนใจ
- strip() คือเมธอดที่ตัดคำออกจากหัวและท้ายของข้อความ ในกรณีที่ไม่ระบุจะเป็นการตัดการเว้นวรรค (whitespace)
Coding
str = " Hello World " print(str,"->",str.strip()) str = "***Hello*World***" print(str,"->",str.strip('*'))
ผลลัพท์
Hello World -> Hello World ***Hello*World*** -> Hello*World
2. lower() คือเมธอดเปลี่ยนข้อความภาษาอังกฤษเป็นตัวพิมพ์เล็กทั้งหมด
Coding
str = "Hello World" print(str,"->",str.lower())
ผลลัพท์
Hello World -> hello world
3. upper() คือเมธอดเปลี่ยนข้อความภาษาอังกฤษเป็นตัวพิมพ์ใหญ่ทั้งหมด
Coding
str = "Hello World" print(str,"->",str.upper())
ผลลัพท์
Hello World -> HELLO WORLD
4. replace() คือเมธอดเปลี่ยนข้อความเป็นข้อความอื่นตามที่กำหนด
Coding
str = "Hello World" print(str,"->",str.replace('World','Python'))
ผลลัพท์
Hello World -> Hello Python
5. split() คือเมธอดแบ่งข้อความออกเป็นข้อความย่อย โดยสามารถระบุอักษรที่ใช้ในการแบ่งได้ด้วย
Coding
str = "Hello World" print('str ->' , str.split()) str2 = "Hello,World" print('str2 ->' , str2.split(','))
ผลลัพท์
str -> ['Hello', 'World'] str2 -> ['Hello', 'World']
การตรวจสอบข้อความใน String
ถ้าต้องการตรวจสอบหาข้อความย่อย หรือตัวอักษรที่อยู่ในข้อความ สามารถทำได้โดยใช้คำสั่ง in หรือ not in
ตัวอย่าง ต้องการตรวจสอบว่ามีคำว่า ‘programming’ ในข้อความ “Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.” หรือไม่
Coding
text = """Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.""" x = 'programming' in text print(x)
ผลลัพท์
True
ตัวอย่าง ต้องการตรวจสอบว่าไม่มีคำว่า ‘bad’ ในข้อความ “Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.” หรือไม่
Coding
text = """Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.""" x = 'bad' not in text print(x)
ผลลัพท์
True
การเชื่อมต่อข้อความ
ในการเชื่อมต่อข้อความ 2 ข้อความ ให้ใช้เครื่องหมายบวก ‘+’ ในการเชื่อม
Coding
a = "Hello" b = "World" print(a + b) print(a + ' ' + b) str = "My name is " name = "Max" print(str+name)
ผลลัพท์
HelloWorld Hello World My name is Ma
การทำสำเนาซ้ำ
ในการแสดงข้อความซ้ำต่อกัน ให้ใช้เครื่องหมายดอกจัน ‘*’ ตามด้วยจำนวนที่ต้องการให้ข้อความนั่นซ้ำ
Coding
a = "Thank you! " print(a*3)
ผลลัพท์
Thank you! Thank you! Thank you!
การจัดรูปแบบ String
ในภาษา python เราสามารถจัดรูปแบบ หรือแทรกข้อความย่อยลงในข้อความได้ โดยใช้เมธอด format()
Coding
quantity = 3 itemno = 567 price = 49.95 myorder = "I want {} pieces of item {} for {} dollars." print(myorder.format(quantity, itemno, price))
ผลลัพท์
I want 3 pieces of item 567 for 49.95 dollars.
หรือสามารถกำหนด index ของข้อความที่ต้องการแทรกได้ด้วย
Coding
text = """{0} is an interpreted, high-level, general-purpose programming {1}. Created by Guido van Rossum and first released in 1991, {0}'s design philosophy emphasizes code readability with its notable use of significant whitespace. Its {1} constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.""" print(text.format('Python','language'))
ผลลัพท์
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects. Process finished with exit code 0
Escape Character
Escape Character คือรหัสคำสั่งที่ใช้ควบคุมการแสดงผล ใช้ร่วมกับฟังก์ชัน print
Code | Result |
---|---|
\’ | Single Quote |
\” | Double Quote |
\\ | Backslash |
\n | New Line |
\r | Carriage Return |
\t | Tab |
\b | Backspace |
\f | Form Feed |
\ooo | Octal value |
\xhh | Hex value |
Coding
text = "String literals in python are surrounded by either single quotation marks\n or double quotation marks.\n\'hello\' is the same as \"hello\".\nYou can display a string literal with the print() function" print(text)
ผลลัพท์
String literals in python are surrounded by either single quotation marks , or double quotation marks. 'hello' is the same as "hello". You can display a string literal with the print() function