python print如何使用

原创
admin 5小时前 阅读数 2 #Python

Python中print函数的使用

Python中的print函数是一个非常重要的函数,它用于在控制台上输出信息,print函数可以接受任意数量和类型的参数,并将它们转换为字符串形式输出,下面是一些使用print函数的例子:

1、输出字符串

print("Hello, World!")

2、输出整数值

print(5)

3、输出浮点数值

print(3.14)

4、输出列表

print([1, 2, 3])

5、输出字典

print({"name": "John", "age": 25})

除了以上简单的例子外,print函数还有很多其他的用法,你可以使用它来输出格式化字符串、输出表格、输出图像等等,下面是一些更高级的用法:

1、输出格式化字符串

name = "John"
age = 25
print("My name is %s and I am %d years old." % (name, age))

2、输出表格

headers = ["Name", "Age", "Gender"]
rows = [["John", 25, "Male"], ["Jane", 30, "Female"], ["Bob", 40, "Male"]]
for row in rows:
    print("%-10s %-10s %-10s" % row)

3、输出图像

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
热门