在Python中,输出结果通常有以下几种方法:
1. 使用`print()`函数:
```python
a = 10
b = 15
print(a + b) 输出:25
2. 使用`return`语句在函数中返回结果:
```python
def add_numbers(a, b):
return a + b
result = add_numbers(10, 5)
print(result) 输出:15
3. 使用变量保存结果:
```python
result = 10 + 5
print(result) 输出:15
4. 使用`f-string`格式化字符串:
```python
name = "John"
age = 30
print(f"Name: {name}, Age: {age}") 输出:Name: John, Age: 30
5. 使用内建输出函数,如`str()`, `repr()`, `int()`, `float()`, `bool()`等:
```python
x = 42
print(str(x)) 输出:'42'
print(repr(x)) 输出:'42'
6. 自定义输出函数:
```python
def custom_print(obj):
print(f"Custom output for {obj}")
custom_print(10) 输出:Custom output for 10
7. 使用文件对象进行输出:
```python
with open('output.txt', 'w') as file:
file.write("Hello, World!") 将字符串写入文件
以上方法都可以用来在Python中输出结果。选择哪种方法取决于你的具体需求