Python print函数参数详解



官方文档

    print(…)
    print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep: string inserted between values, default a space.
    end: string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

参数解析
value:需要输出的值,可以是多个,用”,”分隔。
sep:多个输出值之间的间隔,默认为一个空格。
end:输出语句结束以后附加的字符串,默认是换行(’\n’)。
file:输出的目标对象,可以是文件也可以是数据流,默认是“sys.stdout”。
flush:flush值为True或者False,默认为Flase,表示是否立刻将输出语句输出到目标对象。

演示

默认:print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

>>> print("hello world")
hello world

    1
    2

当value有多个:

>>> print("hello","world")
hello world

    1
    2

当sep为”,”

>>> print("hello","world",sep=",")
hello,world

    1
    2

当end为“”

>>>print("hello","world",end="")
>>>print("hello","world")
hello worldhello world

    1
    2
    3

当file指向test.txt

test = open("test.txt", "w")
print("hello","world",sep="\n", file=test)

    1
    2
    3

此时当前目录下会新建一个test.txt文件里面内容为

hello
world

    1
    2

flush=False
该参数只有两个选项True or False。
当flush=False时,输出值会存在缓存,然后在文件被关闭时写入。
当flush=True时,输出值强制写入文件。
原文:https://blog.csdn.net/weixin_41939225/article/details/79782793

转载于:https://www.cnblogs.com/fmgao-technology/p/9071156.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,print函数用于将文本或变量输出到控制台。在Python 3.x版本中,print是一个函数,而在Python 2.x版本中,print是一个语句。因此,使用print函数的方式在这两个版本中略有不同。在Python 3.x中,使用print函数时,需要将要打印的内容放在括号中。例如,print("Hello World")将会打印出"Hello World"这个字符串。 在使用print函数时,还可以使用格式化字符串来打印变量的值。比如,print("Hello %s" % variable)中的%s表示一个占位符,variable是一个包含字符串的变量。这样,print函数会将字符串中的占位符替换为变量的值。 总结起来,print函数用于将文本或变量输出到控制台,它可以通过括号将要打印的内容括起来,还可以使用格式化字符串来打印变量的值。在Python 2.x版本中,print是一个语句,在Python 3.x版本中,print是一个函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python基础(九)——print函数详解,配合参数示例详解](https://blog.csdn.net/qq_42659468/article/details/118650165)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [详解Python Print函数](https://blog.csdn.net/2301_77669908/article/details/130406031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值