在Python中,输出通常使用`print()`函数完成。以下是使用`print()`函数输出的一些基本方法:
基本输出
```python
print("Hello, World!") 输出字符串
print(123) 输出数字
name = "Tom"
print("My name is", name) 输出变量
格式化输出
使用`%`操作符:
```python
age = 25
print("I am %d years old." % age) 使用%d表示整数
使用`.format()`方法:
```python
print("My age is {}.".format(age)) 使用{}作为占位符
使用f-string(Python 3.6及以上版本):
```python
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}") 使用花括号进行变量替换
输出到文件
```python
with open("output.txt", "w") as file:
file.write("Hello, file!") 将文本写入文件
以上是Python中输出内容的基本方法。