อยากเพิ่มค่าเข้า Python string? ไม่ต้องหาไกลกว่า %s operator ตัวดำเนินการนี้ format ค่าภายใน string %s syntax น่ารักกว่า concatenation operator ที่คุณคุ้นเคย
คู่มือนี้พูดถึงว่า %s symbol หมายถึงอะไร ทำงานยังไง พร้อมตัวอย่าง ช่วยเรียนรู้การใช้ในโค้ด
%s Operator คืออะไร?
%s operator เพิ่ม string เข้าไปในอีก string หนึ่ง
ที่นี่ เราเขียนโปรแกรมคำนวณความเร็วเฉลี่ยของรถเดินทางถึงจุดหมาย ใช้สูตร:
speed = distance / time
เริ่มถามผู้ใช้ ระยะทาง เวลาที่ใช้ และไปที่ไหน:
distance = input("How far did you travel (in miles)? ")
time = input("How long did it take you to reach your destination (in hours)? ")
where = input("Where were you going? ")
คำนวณความเร็วเฉลี่ย:
speed = round(float(distance) / float(time), 2)
แปลง "distance" และ "time" เป็น floating point เพื่อคำนวณทางคณิตศาสตร์ ปัดผลลัพธ์ speed เป็น 2 ตำแหน่ง
ตอนมีค่าแล้ว แจ้งผู้ใช้ใน Python console ความเร็วเฉลี่ย ใช้ string formatting:
print("On your journey to %s, you drove at an average speed of %s miles per hour." % (where, speed))
%s syntax มี 3 ส่วน:
- %s operator คือตำแหน่งที่ string values จะถูกเพิ่ม
- % (where, speed) ระบุว่า values อะไรจะถูกเพิ่มเข้า string
จำนวน values ที่ต้องการเพิ่มเข้า string ต้องเท่ากับที่ระบุใน parenthesis หลัง % operator ท้าย string ไม่เช่นนั้นจะเจอ TypeError: not enough arguments for format string
ในโค้ดเรา เพิ่ม 2 values เข้า string ใช้ %s 2 ครั้ง และมี 2 values ใน parenthesis หลัง % sign
รันโปรแกรม:
How far did you travel? 63 How long did it take you to reach your destination? 2 Where were you going? London On your journey to London, you drove at an average speed of 31.5 miles per hour.
โค้ดคำนวณความเร็วเฉลี่ยสำเร็จ
%s operator แปลงค่าเป็น string อัตโนมัติ หมายถึงไม่ต้องเปลี่ยน data type ของ "speed" ตอน format
The % String Formatting Syntax
% string formatting syntax มีอะไรมากกว่าแค่ %s operator สามารถใช้ % syntax format numbers ใน string ได้อีก
อยากเรียนรู้ format numbers ด้วย % formatting syntax อ่าน Python documentation for string formatting เรายังเขียน tutorial ปัด 2 ตำแหน่งด้วย % operator
วิธี String Formatting ใหม่ๆ
ตั้งแต่ format() syntax มาใน Python 2.6 % string formatting operand ลดความนิยมลงเยอะ
เพราะ formatting syntax powerful กว่า และ format() syntax ไมยากเลย ตัวอย่าง statement:
print("On your journey to {}, you drove at an average speed of {} miles per hour.".format(where, speed))
Statement นี้พิมพ์ข้อความเดียวกับที่สร้างก่อนหน้า ใช้ .format() syntax ใส่ "where" และ "speed" เข้า string
.format() syntax ทำให้ตั้งชื่อแต่ละ value ที่เพิ่มเข้า string ได้ ฟีเจอร์นี้ %s syntax ไม่มี
ใน Python 3 มี f strings เป็น alternative อีกตัวสำหรับ % syntax F strings วิธีง่าย embed value หรือ expression เข้า string ด้วย string literals เรียนรู้ f strings เพิ่มในบทความ Python 3 f strings
สรุป
%s operator ให้คุณเพิ่มค่าเข้า Python string %s บ่งบอกว่าต้องการเพิ่ม string value เข้า string % operator ใช้กับ configurations อื่น เช่น %d format ค่าประเภทต่างๆ ได้
ใน Python versions ใหม่ๆ % syntax นิยมลดลง เลือก f strings และ format() method แทน
ตอนนี้คุณมีความรู้ใช้ %s operator ในโค้ดอย่าง Python developer มืออาชีพแล้ว!