Python พิมพ์ ()ฟังก์ชั่น
ตัวอย่าง
พิมพ์ข้อความลงบนหน้าจอ:
print("Hello World")
ความหมายและการใช้งาน
ฟังก์ชัน นี้print()
จะพิมพ์ข้อความที่ระบุไปยังหน้าจอหรืออุปกรณ์ส่งออกมาตรฐานอื่นๆ
ข้อความสามารถเป็นสตริงหรือวัตถุอื่น ๆ วัตถุจะถูกแปลงเป็นสตริงก่อนที่จะเขียนลงบนหน้าจอ
ไวยากรณ์
print(object(s),
sep=separator, end=end, file=file, flush=flush)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
object(s) | Any object, and as many as you like. Will be converted to string before printed |
sep='separator' | Optional. Specify how to separate the objects, if there is more than one. Default is ' ' |
end='end' | Optional. Specify what to print at the end. Default is '\n' (line feed) |
file | Optional. An object with a write method. Default is sys.stdout |
flush | Optional. A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
พิมพ์มากกว่าหนึ่งวัตถุ:
print("Hello", "how are you?")
ตัวอย่าง
พิมพ์ทูเพิล:
x = ("apple", "banana", "cherry")
print(x)
ตัวอย่าง
พิมพ์สองข้อความ และระบุตัวคั่น:
print("Hello", "how are you?", sep="---")