บางตัวเลขต้องขึ้นต้นด้วยศูนย์หรือหลายศูนย์ เช่น user ID อาจต้องมีศูนย์จำนวนหนึ่งนำหน้า ถ้าต่ำกว่าตัวเลขที่กำหนด ให้ ID numbers ทุกตัวยาวเท่ากัน
Pad string ด้วยศูนย์ใช้ zfill() function Pad number ด้วยศูนย์ใช้ string formatting คู่มือนี้แนะนำตัวอย่าง pad zeros ให้ string และ integer
Python: Pad Zeros ให้ String
เขียนโปรแกรมตรวจสอบว่าเลข raffle ticket เป็นรางวัลหรือไม่ แต่ละ ticket มี 3 ตัวเลขแบบนี้:
022
โปรแกรมขอผู้ใช้ใส่ตัวเลข ถ้าตัวเลขที่ใส่ น้อยกว่า 100 จะเพิ่มศูนย์นำหน้า เพื่อให้โปรแกรมเปรียบเทียบกับ winning ticket numbers ได้
เริ่มกำหนดลิสต์ winning ticket numbers:
winners = ["033", "087", "183", "173", "012"]
แต่ละ ticket number format เป็น string ถามผู้ใช้ใส่ตัวเลขด้วย input():
to_check = input("Insert a number that you want to check: ")
ตัวเลขที่ผู้ใช้ใส่ คือตัวที่จะหาใน winners list ค่านี้ format เป็น string
ผู้ใช้อาจไม่ใส่ตัวเลข 3 หลัก ให้แน่ใจว่า raffle number ที่ใส่ format ถูกต้อง ใช้ zfill() method เพิ่มศูนย์นำหน้าตัวเลข
เพราะ raffle ticket numbers ยาว 3 ตัวอักษร ใช้ค่า 3 เป็น parameter กับ zfill():
to_check = to_check.zfill(3)
Method นี้ทำให้ "to_check" format ถูกต้อง ค่า 3 สะท้อนว่าต้องมีตัวเลขกี่ตัวใน "to_check" ไม่ใช่จำนวนศูนย์ที่ pad
ใช้ "if" statement กับ "in" operator เช็คว่า "to_check" อยู่ใน winners list ไหม:
if to_check in winners:
print("Ticket #{} is a winner.".format(to_check))
else:
print("Ticket #{} is not a winner.".format(to_check))
ถ้า ticket ตรง winning number if statement รัน ไม่งั้น else statement รัน รันโค้ด:
Insert a number that you want to check: 33 Ticket #033 is a winner.
โค้ดระบุ ticket #033 เป็นรางวัลสำเร็จ ดูถ้าใส่ตัวเลขไม่ตรง winning ticket:
Insert a number that you want to check: 182 Ticket #182 is not a winner.
โปรแกรมบอกว่า ticket ไม่ใช่รางวัล
Python: Pad Zeros ให้ Number
Note: ตัวอย่างด้านล่างทำงานได้ใน Python 2.7 และเวอร์ชันหลังๆ เท่านั้น
ตัวอย่างแรก pad zeros ให้ค่าที่เก็บเป็น string ถ้าต้องการ pad zeros ให้ number มีอีก approach
สมมติ generate employee identifier codes ใช้ระบุพนักงานแต่ละคน แต่ละ ID ควรขึ้นต้น 3 ตัวเลข และลงท้าย surname ของพนักงาน
เริ่มถามผู้ใช้ตัวเลขนำหน้า employee ID และชื่อพนักงาน:
id_numbers = int(input("Enter the ID number of the employee: "))
surname = input("Enter the surname of the employee: ")
format ค่าเหล่านี้เป็น string ใช้ .format() method:
print("The employee identifier for {} is {:03d}{}.".format(surname, id_numbers, surname))
Curly braces แทนค่าที่จะแทนใน string
Curly braces ชุดแรก: surname พนักงาน ชุดที่สอง: "id_numbers" ถ้าตัวเลขที่ใส่ น้อยกว่า 3 หลัก จะเพิ่มศูนย์เข้าไป ชุดสุดท้าย: ส่วนท้าย identifier คือ surname
รันโปรแกรม:
Enter the ID number of the employee: 823 Enter the surname of the employee: SMITH The employee identifier for SMITH is 823SMITH.
โปรแกรมทำงาน กรณีนี้ใส่ ID number 3 หลัก ลองใส่ ID number 2 หลัก:
Enter the ID number of the employee: 78 Enter the surname of the employee: JONES The employee identifier for JONES is 078JONES.
โปรแกรมยัง generate identifier ใน format ที่อธิบาย ทุก identifier มี 3 ตัวเลข ตามด้วย surname
สรุป
ใช้ zfill() method pad zeros นำหน้า Python string ใช้ .format() method pad zeros ให้ integer value
ตอนนี้คุณพร้อมเพิ่มศูนย์นำหน้าค่าใน Python อย่าง expert coder แล้ว!