什么是打印功能中的结尾逗号? [英] What's ending comma in print function for?

查看:36
本文介绍了什么是打印功能中的结尾逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码来自 http://docs.python.org/2/tutorial/errors.html#predefined-clean-up-actions

with open("myfile.txt") as f:
    for line in f:
        print line,

我不明白的是打印命令末尾的 , 是什么.

What I don't understand is what's that , for at the end of print command.

我还检查了文档,http://docs.python.org/2/library/functions.html#print.

理解不够,是不是写错了?(好像不是,来自官方教程).

Not understanding enough, is it a mistake?(it seems not. it's from the official tutorial).

我来自 ruby​​/javascript,这对我来说很不寻常.

I am from ruby/javascript and it's unusual for me.

推荐答案

在python 2.7中,逗号是为了表示字符串会打印在同一行

In python 2.7, the comma is to show that the string will be printed on the same line

例如:

for i in xrange(10):
     print i,

这将打印

1 2 3 4 5 6 7 8 9 

要在 python 3 中执行此操作,您将执行以下操作:

To do this in python 3 you would do this:

 for i in xrange(10):
      print(i,end=" ")

你可能会发现这个答案很有帮助

You will probably find this answer helpful

在 python 中水平打印

---- 编辑---

文档,http://docs.python.org/2/reference/simple_stmts.html#the-print-statement,说

'\n' 字符写在末尾,除非打印语句以逗号结尾.

A '\n' character is written at the end, unless the print statement ends with a comma.

这篇关于什么是打印功能中的结尾逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆