การจัดรูปแบบสตริง#

การแนะนำ#

เมธอด .format() ใน Python ใช้สำหรับสร้างสตริงที่มีตัวแปร นิพจน์ หรือค่าต่างๆ เมธอดนี้จะแทนที่ตัวยึดตำแหน่งวงเล็บปีกกา {} ในสตริงด้วยค่าที่ให้ไว้ภายใน .format()

def main():
    x = 1
    y = 5

    # Display the variables using .format()
    console.print("Position: ({}, {})".format(x, y))

# Start threads — Do not delete
start_thread(main)

def main():
    # Display a calculation using .format()
    console.print("Sum: {}".format(5 + 3))

# Start threads — Do not delete
start_thread(main)

def main():
    wait(1, SECONDS)
    # Display the time using .format()
    console.print("Time: {}".format(timer.time(SECONDS)))

# Start threads — Do not delete
start_thread(main)

การจัดรูปแบบตัวเลขด้วย .format – แทรกค่าและนิพจน์ลงในสตริง

  • :.xf – กำหนดจำนวนตำแหน่งทศนิยมที่จะแสดง

  • round – ปัดเศษตัวเลขให้เหลือจำนวนทศนิยมที่กำหนด

  • :, – เพิ่มเครื่องหมายจุลภาคเป็นตัวคั่นหลักพัน

  • :.x% – แปลงเลขฐานสิบเป็นเปอร์เซ็นต์โดยมีทศนิยม x ตำแหน่ง

  • :#x – จัดรูปแบบตัวเลขเป็นเลขฐานสิบหก

  • :b – จัดรูปแบบตัวเลขเป็นเลขฐานสอง

การรวมสตริง – รวมข้อความและค่าต่างๆ เข้าด้วยกัน

  • .format() – รวมสตริงและตัวแปรในนิพจน์เดียว

  • + operator – เชื่อมต่อสตริงด้วยตนเองโดยมีการแปลงประเภทเพิ่มเติม

เมธอดของสตริง – เปลี่ยนตัวพิมพ์ใหญ่เล็กของข้อความ

  • upper() – แปลงอักขระทั้งหมดเป็นตัวพิมพ์ใหญ่

  • lower() – แปลงอักขระทั้งหมดเป็นตัวพิมพ์เล็ก

การตรวจสอบสตริงย่อย – ทดสอบการมีอยู่หรือตำแหน่งของข้อความ

  • in – ตรวจสอบว่าคำนั้นมีอยู่ในสตริงหรือไม่

  • startswith() – ตรวจสอบว่าสตริงเริ่มต้นด้วยค่าที่กำหนดหรือไม่

  • endswith() – ตรวจสอบว่าสตริงลงท้ายด้วยค่าที่กำหนดหรือไม่

ลำดับการหลีกเลี่ยง – จัดรูปแบบเอาต์พุตด้วยอักขระพิเศษ

  • \n – เพิ่มการขึ้นบรรทัดใหม่ (บรรทัดใหม่)

  • \t – เพิ่มช่องว่างแท็บระหว่างรายการ

การจัดรูปแบบตัวเลขด้วย .format#

.format สามารถใช้เพื่อควบคุมวิธีการแสดงตัวเลข รวมถึงจำนวนทศนิยม การปัดเศษ และรูปแบบการจัดรูปแบบอื่นๆ เช่น:

Fixed Decimal Places#

:.xf ควบคุมจำนวนตำแหน่งทศนิยมที่จะแสดงตัวเลข

Usage:
:.xf

พารามิเตอร์

คำอธิบาย

x

จำนวนตำแหน่งทศนิยมที่จะแสดง

def main():
    # Display pi with 2 decimal places
    pi = 3.1415926535
    console.print("Pi: {:.2f}".format(pi))
    # Output: Pi: 3.14

# Start threads — Do not delete
start_thread(main)

Rounding Numbers#

round จะปัดเศษตัวเลขให้เหลือจำนวนทศนิยมที่กำหนดก่อนทำการจัดรูปแบบ

Usage:
round(number, x)

พารามิเตอร์

คำอธิบาย

number

ตัวเลขที่จะปัดเศษ

x

จำนวนตำแหน่งทศนิยมที่จะปัดเศษ

def main():
    # Display a value rounded to only 2 decimal places
    value = 5.6789
    console.print("Rounded: {}".format(round(value, 2)))
    # Output: Rounded: 5.68

# Start threads — Do not delete
start_thread(main)

Thousands Separator#

:, แทรกเครื่องหมายจุลภาคเป็นตัวคั่นหลักพันเพื่อให้ตัวเลขขนาดใหญ่สามารถอ่านได้ง่ายขึ้น

Usage:
:,

พารามิเตอร์

คำอธิบาย

ตัวระบุรูปแบบนี้ไม่มีพารามิเตอร์

def main():
    # Display a large number separated with commas
    number = 1234567
    console.print("Formatted: ")
    console.new_line()
    console.print("{:,}".format(number))
    # Output: Formatted: 1,234,567

# Start threads — Do not delete
start_thread(main)

Percentage#

:.x% จะแปลงค่าทศนิยมเป็นค่าเปอร์เซ็นต์โดยการคูณค่าด้วย 100 แล้วเพิ่มเครื่องหมายเปอร์เซ็นต์

Usage:
:.x%

พารามิเตอร์

คำอธิบาย

x

จำนวนตำแหน่งทศนิยมที่จะแสดง

def main():
    # Display a converted decimal to a percentage
    value = 0.875
    console.print("Score: {:.1%}".format(value))
    # Output: Score: 87.5%

# Start threads — Do not delete
start_thread(main)

Hexadecimal#

:#x แปลงตัวเลขเป็นเลขฐานสิบหก

Usage:
:#x

พารามิเตอร์

คำอธิบาย

ตัวระบุรูปแบบนี้ไม่มีพารามิเตอร์

def main():
    # Convert 255 to hexadecimal
    number = 255
    console.print("Hex: {:#x}".format(number))
    # Output: Hex: 0xff

# Start threads — Do not delete
start_thread(main)

Binary#

:b แปลงตัวเลขเป็นเลขฐานสอง (ฐาน 2)

Usage:
:b

พารามิเตอร์

คำอธิบาย

ตัวระบุรูปแบบนี้ไม่มีพารามิเตอร์

def main():
    # Convert 3 to binary
    console.print("Binary: {:b}".format(3))
    # Output: Binary: 11

# Start threads — Do not delete
start_thread(main)

การรวมสตริง#

คุณสามารถรวม (หรือต่อ) สตริงได้โดยใช้สองวิธี:

Using .format#

แทรกค่าลงในสตริงโดยตรงโดยใช้ {}

def main():
    # Display an answer based on the given emotion
    emotion = "good"
    console.print("I'm {}, you?".format(emotion))

# Start threads — Do not delete
start_thread(main)

+ Operator#

รวมชิ้นส่วนหลายชิ้นเข้าด้วยกันโดยใช้ +

หมายเหตุ: ข้อมูลที่ไม่ใช่สตริงจะต้องถูกแปลงเป็นสตริงก่อนโดยใช้ str()

def main():
    # Display the x and y values
    x = 10
    y = 20
    console.print("X: " + str(x) + ", Y: " + str(y))

# Start threads — Do not delete
start_thread(main)

เมธอดสตริง#

Python มีเมธอดในตัวสำหรับแก้ไขและตรวจสอบสตริง

upper#

upper แปลงตัวอักษรทั้งหมดในสตริงให้เป็นตัวพิมพ์ใหญ่

Usage:
upper()

พารามิเตอร์

คำอธิบาย

เมธอดนี้ไม่มีพารามิเตอร์

def main():
    # Capitalize a string with .upper()
    message = "vexcode"
    console.print(message.upper())  # Output: VEXCODE

# Start threads — Do not delete
start_thread(main)

lower#

lower แปลงตัวอักษรทั้งหมดในสตริงให้เป็นตัวพิมพ์เล็ก

Usage:
lower()

พารามิเตอร์

คำอธิบาย

เมธอดนี้ไม่มีพารามิเตอร์

def main():
    # Make a string lowercase with .lower()
    message = "VEXCODE"
    console.print(message.lower())  # Output: vexcode

# Start threads — Do not delete
start_thread(main)

การตรวจสอบสตริงย่อย#

in#

in เป็นคีย์เวิร์ดที่ส่งค่าบูลีนกลับมาเพื่อระบุว่าสตริงย่อย (คำหรือส่วนของข้อความ) นั้นมีอยู่ในสตริงหลักหรือไม่

  • True – คำนั้นมีอยู่ในสตริง

  • False – คำนั้นไม่มีอยู่ในสตริง

def main():
    message = "Hey everyone!"
    if "Hey" in message:
        console.print("Hello!")

# Start threads — Do not delete
start_thread(main)

startswith#

startswith ส่งคืนค่าบูลีนที่ระบุว่าสตริงเริ่มต้นด้วยค่าที่กำหนดหรือไม่

  • True – คำนั้นเป็นคำแรกของสตริง

  • False – คำนั้นไม่ได้ขึ้นต้นสตริง

Usage:
startswith(substring)

พารามิเตอร์

คำอธิบาย

substring

ส่วนย่อยที่จะตรวจสอบที่จุดเริ่มต้นของสตริง

def main():
    # Check for 'GO' at the start of a string
    message = "GO Robot"

    if message.startswith("GO"):
        console.print("GO first!")

# Start threads — Do not delete
start_thread(main)

endswith#

endswith ส่งคืนค่าบูลีนที่ระบุว่าสตริงลงท้ายด้วยค่าที่กำหนดหรือไม่

  • True – คำนั้นเป็นคำสุดท้ายของสตริง

  • False – คำนั้นไม่ใช่คำสุดท้ายของสตริง

Usage:
endswith(substring)

พารามิเตอร์

คำอธิบาย

substring

ส่วนย่อยของสตริงที่จะตรวจสอบที่ส่วนท้ายของสตริง

def main():
    # Check for `Robot` at the end of a string
    message = "GO Robot"

    if message.endswith("Robot"):
        console.print("Robot last!")

# Start threads — Do not delete
start_thread(main)

ลำดับการหลบหนี#

ลำดับการหลีกเลี่ยง (Escape sequences) คืออักขระพิเศษที่ใช้ภายในสตริงเพื่อจัดรูปแบบข้อความที่แสดงผล

New Line#

\n จะย้ายข้อความขึ้นบรรทัดใหม่เมื่อพิมพ์

def main():
    # Display text on two lines
    console.print("First line\nSecond line")

# Start threads — Do not delete
start_thread(main)

Tab Spacing#

\t จะแทรกช่องว่างแท็บระหว่างคำหรือตัวเลข

def main():
    # Display the quantity of disks
    quantity = 2
    console.print("Disks:\t", quantity)

# Start threads — Do not delete
start_thread(main)