在Python中,要在屏幕上输出文本,你可以使用 `print` 函数。下面是一些基本的使用方法:
1. 输出单个字符串:
print("Hello, World!")
2. 输出带有换行符的字符串:
print("Hello, World!\n")
3. 输出多个字符串,用加号连接:
print("Hello,")
print("World!")
4. 使用格式化字符串(f-string):
name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")
5. 设置文本颜色(适用于大多数终端):
RED = "\033[91m"
GREEN = "\033[92m"
BLUE = "\033[94m"
RESET = "\033[0m"
print(f"{RED}This is red text{RESET}")
6. 控制光标位置(适用于支持ANSI转义序列的终端):
def move_cursor(x, y):
print(f"\033[{y};{x}H", end="")
move_cursor(10, 5)
print("Text at specific position")
请注意,上述代码中的颜色代码和光标控制代码可能在不同操作系统和终端中的表现不同。在Windows系统上,可能需要安装 `windows-curses` 模块来支持这些功能。