问Python如何输出变量的值?

答 1 个回答|262 次阅读
Coco老师 - 官方公众号:青少儿编程学习网
擅长:编程教育网站:https://kidscodes.cn/

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)

推荐课程 »更多

    推荐知识

    Python精选库大全,青少年Python编程学习总结

    Python最适合青少儿进阶学习的编程语言