在Python中,输出结果通常有以下几种方法:
1. 使用`print()`函数:
a = 10b = 15print(a + b) 输出:25
2. 使用`return`语句在函数中返回结果:
def add_numbers(a, b):return a + bresult = add_numbers(10, 5)print(result) 输出:15
3. 使用变量保存结果:
result = 10 + 5print(result) 输出:15
4. 使用`f-string`格式化字符串:

name = "John"age = 30print(f"Name: {name}, Age: {age}") 输出:Name: John, Age: 30
5. 使用内建输出函数,如`str()`, `repr()`, `int()`, `float()`, `bool()`等:
x = 42print(str(x)) 输出:'42'print(repr(x)) 输出:'42'
6. 自定义输出函数:
def custom_print(obj):print(f"Custom output for {obj}")custom_print(10) 输出:Custom output for 10
7. 使用文件对象进行输出:
with open('output.txt', 'w') as file:file.write("Hello, World!") 将字符串写入文件
