共計 409 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Python 中,print 函數可以按不同的方法進行操作。以下是常見的 print 操作方法:
- 打印字符串:
print("Hello, World!")
- 打印變量的值:
x = 10
print(x)
- 打印多個變量的值:
x = 10
y = 20
print(x, y)
- 格式化輸出:
name = "Alice"
age = 25
print("My name is %s and I am %d years old." % (name, age))
- 使用 sep 參數指定分隔符:
x = 10
y = 20
print(x, y, sep=", ")
- 使用 end 參數指定結尾字符:
print("Hello", end=" ")
print("World")
輸出:Hello World
- 將輸出重定向到文件:
with open("output.txt", "w") as f:
print("Hello, World!", file=f)
以上是一些常見的 print 操作方法,可以根據具體需求選擇適合的方法。
丸趣 TV 網 – 提供最優質的資源集合!
正文完