📜  Python打印()函数

📅  最后修改于: 2022-05-13 01:55:21.395000             🧑  作者: Mango

Python打印()函数

Pythonprint()函数顾名思义用于打印在Python Python对象(一个或多个)作为标准输出。

示例 1:打印Python对象

Python3
# sample python objetcs
list = [1,2,3]
tuple = ("A","B")
string = "Geeksforgeeks"
 
# printing the objects
print(list,tuple,string)


Python3
# sample python objetcs
list = [1,2,3]
tuple = ("A","B")
string = "Geeksforgeeks"
 
# printing the objects
print(list,tuple,string, sep="<<..>>")


Python3
# sample python objetcs
list = [1,2,3]
tuple = ("A","B")
string = "Geeksforgeeks"
 
# printing the objects
print(list,tuple,string, end="<<..>>")


Python3
# open and read the file
 my_file = open("geeksforgeeks.txt","r")
   
# print the contentts of the file
print(my_file.read())


Python3
# Python code for printing to stderr
 
# importing the package
# for sys.stderr
import sys
 
# variables
Company = "Geeksofrgeeks.org"
Location = "Noida"
Email = "contact@geeksforgeeks.org"
 
# print to stderr
print(Company, Location, Email, file=sys.stderr)


输出:

[1, 2, 3] ('A', 'B') Geeksforgeeks

示例 2:使用分隔符打印对象

蟒蛇3

# sample python objetcs
list = [1,2,3]
tuple = ("A","B")
string = "Geeksforgeeks"
 
# printing the objects
print(list,tuple,string, sep="<<..>>")

输出:



[1, 2, 3]<<..>>('A', 'B')<<..>>Geeksforgeeks

示例 3:指定要在末尾打印的字符串

蟒蛇3

# sample python objetcs
list = [1,2,3]
tuple = ("A","B")
string = "Geeksforgeeks"
 
# printing the objects
print(list,tuple,string, end="<<..>>")

输出:

[1, 2, 3] ('A', 'B') Geeksforgeeks<<..>>

示例 4:打印和读取外部文件的内容

为此,我们还将使用Python open()函数,然后打印其内容。我们已经在系统中保存了以下名为geeksforgeeks.txt 的文本文件

要阅读和打印此内容,我们将使用以下代码:

蟒蛇3

# open and read the file
 my_file = open("geeksforgeeks.txt","r")
   
# print the contentts of the file
print(my_file.read())

输出:

示例 5:打印到 sys.stderr

蟒蛇3

# Python code for printing to stderr
 
# importing the package
# for sys.stderr
import sys
 
# variables
Company = "Geeksofrgeeks.org"
Location = "Noida"
Email = "contact@geeksforgeeks.org"
 
# print to stderr
print(Company, Location, Email, file=sys.stderr)

输出:

Geeksofrgeeks.org Noida contact@geeksforgeeks.org