1 个回答|262 次阅读
Python 的 print()函数通常用于输出变量。
例:
x = "Python is good"
print(x)
1、在print()函数中,输出多个变量,以逗号分隔:
例:
x = "Python"
y = "is"
z = "good"
print(x, y, z)
2、还可以使用运算符+输出多个变量:
例
x = "Python "
y = "is "
z = "good"
print(x + y + z)
请注意x,y变量对应值后面的空格字符,如果没有它们,结果将是“Pythonisgood”。
3、对于数字,+字符用作数学运算符:
例:
x = 5
y = 10
print(x + y)
4、在print()函数中,当将字符串和数字用+运算符组合在一起时,Python会给一个错误(TypeError: unsupported operand type(s) for +: 'int' and 'str'):
例
x = 5
y = "John"
print(x + y)
5、在print()函数中输出多个变量的最佳方法是用逗号分隔它们,支持不同的数据类型:
例
x = 5
y = "John"
print(x, y)
请先 登录 后评论