Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ทำไม Backslash ถึงแสดงสองตัวเมื่อ Print ใน Python

Backslash เป็น escape character ใช้ escape quotes, newline ฯลฯ

เวลา print สตริงที่มี backslash จะเห็น 2 ตัว เพราะ backslash ตัวแรก escape backslash ตัวที่สอง (บอก interpreter ว่าตัวนี้เป็น backslash ธรรมดา ไม่ใช่ escape char)

ตัวอย่าง

print('hello \\ John\'s friends')

ผลลัพธ์

hello \ John's friends