我知道如何在Python3.0中使用end=""和print函数来抑制linefeed,但是如何在使用str.format方法时抑制linefeed呢?
print("Hello", end=",") # Works
print(" Hola")
print("{}".format(""))
print("{}".format("Hello", end = ",")) # Doesn't work
print("{}".format(" Hola"))发布于 2015-02-05 21:36:53
换行符是由print函数而不是str.format添加的。来自文档
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)将objects打印到文本流file,由sep分隔,然后再打印end。
注意end参数如何默认为'\n' (换行符)。str.format无法删除这条换行符,因为它直到格式化字符串给print之后才会出现。
您将需要使用end=""
print("{}".format("Hello"), end="")https://stackoverflow.com/questions/28354495
复制相似问题