print函数python_Python print()函数

print函数python

print() function is a library function in Python, it is used to print the value of the given arguments (objects) to the standard output stream i.e. to print it on the screen.

print()函数是Python中的一个库函数,用于将给定参数(对象)的值打印到标准输出流,即在屏幕上打印它。

General syntax:

通用语法:

print(text/object)

Full syntax:

完整语法:

print(objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Parameter(s):

参数:

  • objects: The value or the variables/objects to be printed on the screen, multiple objects can be passed separating by the commas (object1, object2, ..., objectN).

    objects :要在屏幕上打印的值或变量/对象,可以用逗号分隔多个对象(object1,object2,...,objectN)

  • sep: It's an optional parameter and is used to specify the separator between the arguments, The default value is space (' ').

    sep :这是一个可选参数,用于指定参数之间的分隔符,默认值为空格('')。

    Read more: sep parameter in print() function.

    阅读更多print()函数中的sep参数

  • end: It's also an optional parameter and is used to specify the value to be printed at the last. The default value is a newline character ('\n').

    end :它也是一个可选参数,用于指定最后要打印的值。 默认值为换行符('\ n')。

    Read more: end parameter in print() function.

    阅读更多print()函数中的end参数

  • file: It's also an optional parameter and is used to specify the file name where we can write the argument values. The default value is sys.stdout.

    file :这也是一个可选参数,用于指定我们可以在其中写入参数值的文件名。 默认值为sys.stdout。

    Read more: file parameter in print() function.

    阅读更多print()函数中的file参数

  • flush: It's also an optional parameter and is used to flush the stream. The default value is "False".

    flush :这也是一个可选参数,用于刷新流。 默认值为“ False”。

    Read more: flush parameter in print() function.

    阅读更多print()函数中的flush参数

Return value:

返回值:

The return type of print() function is NoneType, it returns nothing.

print()函数的返回类型为NoneType ,不返回任何内容。

Example 1: Printing text, values, etc (i.e. without using optional parameters)

示例1:打印文本,值等(即,不使用可选参数)

# Python code to demonstrate the example of 
# print() function without using 
# optional parameters 

# printing strings
print("Hello, world!")
print("How are you?")
print("India","USA", "Russia", "Israel")
print()

# printing mixed value
print("Mike", 21, "USA", 65.50)
print([10, 20, 30]) # list
print({"Mike", 21, "USA", 123.5}) # set
print(123, "Hello", [10, 20, 30]) #number, string, list
print()

# printing text or/and variables
name = "Mike"
age = 21
con = "USA"
w = 65.50

print(name, age, con, w)
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w)

Output:

输出:

Hello, world!
How are you?
India USA Russia Israel

Mike 21 USA 65.5
[10, 20, 30]
{'Mike', 123.5, 'USA', 21}
123 Hello [10, 20, 30]

Mike 21 USA 65.5
Name: Mike Age: 21 Country: USA Weight: 65.5

Example 2: print() with sep parameter

示例2:带有sep参数的print()

# Python code to demonstrate the example of 
# print() function with sep parameter

print("Separated by ','")
print("Mike", 21, "USA", 65.50, sep=',')
print("Separated by ' # '")
print("Mike", 21, "USA", 65.50, sep=' # ')
print()

name = "Mike"
age = 21
con = "USA"
w = 65.50

print("Separated by ','")
print(name, age, con, w, sep=',')
print("Separated by '\n'")
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w, sep='\n')

Output:

输出:

Separated by ','
Mike,21,USA,65.5
Separated by ' # '
Mike # 21 # USA # 65.5

Separated by ','
Mike,21,USA,65.5
Separated by '
'
Name:
Mike
Age:
21
Country:
USA
Weight:
65.5

Example 3: print() with end parameter

示例3:带有end参数的print()

# Python code to demonstrate the example of 
# print() function with end parameter

print("Ended by none i.e. removing default sep value")
print("Hello,", end='')
print("world", end='')


print("Ended by '###\\n'")
print("Mike", 21, "USA", 65.50, end='###\n')
print("Ended by '-END-\\n'")
print("Mike", 21, "USA", 65.50, end='-END-\n')
print()

name = "Mike"
age = 21
con = "USA"
w = 65.50

print("Ended by 'FINISH\\n'")
print(name, age, con, w, end='FINISH\n')
print("Ended by '@@@@'")
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w, end='@@@')

Output:

输出:

Ended by none i.e. removing default sep value
Hello,worldEnded by '###\n'
Mike 21 USA 65.5###
Ended by '-END-\n'
Mike 21 USA 65.5-END-

Ended by 'FINISH\n'
Mike 21 USA 65.5FINISH
Ended by '@@@@'
Name: Mike Age: 21 Country: USA Weight: [email protected]@@

Example 4: print() with file parameter

示例4:带有文件参数的print()

# Python code to demonstrate the example of 
# print() function with file parameter

import sys

print("Printing to sys.stderr")
print("Hello, world!", file = sys.stderr)

print("Printing to an external file")
objF = open("logs.txt", "w")
print("How are you?", file = objF)

objF.close()

Output:

输出:

Printing to sys.stderr
Hello, world!
Printing to an external file

--- logs.txt ---
How are you?

Example 5: print() with flush parameter

示例5:带有flush参数的print()

# Python code to demonstrate the example of 
# print() function with flush parameter

from time import sleep

# output is flushed here
print("Hello, world!", end='', flush= True)
sleep(5)
print("Bye!!!")

# output is not flushed here
print("IncludeHelp", end='')
sleep(5)
print("Okay!!!")

Output:

输出:

Hello, world!Bye!!!
IncludeHelpOkay!!!

See the output – "Hello, world" and "Bye!!!" are printing correctly because before sleep(5) the print() is flushing but "IncludeHelp" and "Okay!!!" are printing together, because print() is not flushing.

看到输出–“你好,世界”和“再见!!!” 之所以能够正确打印,是因为在sleep(5)之前, print( )处于刷新状态,但是“ IncludeHelp”和“ Okay !!!” 一起打印,因为print()没有刷新。

翻译自: https://www.includehelp.com/python/print-function.aspx

print函数python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值