Python的print()函数是一个内置函数,用于在控制台输出文本。这是一个非常基本但非常有用的功能。以下是如何使用print()函数的一些例子:
print("Hello, World!")输出变量的值:x = 5y = 7print("The values of x and y are:", x, y)使用字符串格式化输出变量的值:name = "Alice"age = 30print(f"{name} is {age} years old.")使用逗号分隔多个输出项:print("This", "is", "a", "test.")使用sep参数指定分隔符:print("a", "b", "c", sep=", ")使用end参数指定行尾字符:print("Hello, World!", end="***\n")将输出重定向到文件:with open("output.txt", "w") as f: print("Hello, World!", file=f)这只是print()函数的一些基本用法。通过组合不同的参数和格式化选项,你可以创建各种复杂的输出。


