Why does python3 output without line breaks?

青灯夜游
Release: 2020-10-13 09:52:42
Original
24145 people have browsed it

Python3 method of output without line breaks: Provide an additional named parameter [end=""] as the end parameter to the print() function to clearly mention that the string added at the end of the line should be used as the termination string. .

Why does python3 output without line breaks?

Since python's print() function ends with a newline character by default, it will automatically wrap by default. In Python there is a predefined format and if you use print(variable) then it will automatically go to the next line. So how can I output it without line breaks? The following article will introduce it to you, I hope it will be helpful to you.

How to output without line breaks in Python 3?

In Python 3, the simplest solution is provided, all you have to do is provide an extra parameter end to the print function. Example:

# 使用命名参数“end”指定行尾显式的字符串
print("Hello World!", end ="") #end里没有空格
print("My name is Karim") 
 
# 数组
a = [1, 2, 3, 4] 
   
# 输出数组的元素 
for i in range(4): 
    print(a[i], end =" ")  #end里添加空格
Copy after login

Run:

Why does python3 output without line breaks?

We can use the optional named parameter end to explicitly mention the string that should be added at the end of the line.

Whatever you provide as the end argument will be the terminating string.

So if you provide an empty string, there will be no newlines and no spaces will be appended to the input.

Python print() function

The print() method is used to print output, the most common function.

Syntax:

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

Parameters:

  • objects -- plural, indicating that multiple objects can be output at one time. When outputting multiple objects, they need to be separated by ,.

  • sep -- Used to separate multiple objects. The default value is a space.

  • end -- used to set the ending. The default value is the newline character \n, we can change it to other strings.

  • file -- The file object to be written.

  • flush -- Whether the output is cached is usually determined by the file, but if the flush keyword argument is True, the stream will be forced to be flushed.

Return value

  • None.

The above is the detailed content of Why does python3 output without line breaks?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template