How to print without line breaks in python

爱喝马黛茶的安东尼
Release: 2019-08-01 13:58:54
Original
7165 people have browsed it

How to print without line breaks in python

In Python2.7, after executing print, it will automatically wrap the line. The following code will print: abc\n123\n (where \n represents the line break)

print ('abc')
print ('123')
Copy after login

How to print characters without line breaks. Here are three simple methods to print characters without line breaks in Python 2.7:

1. Add a comma after the print function, and the printing effect will be as if a space is used instead of line breaks. The following code will print: abc123 (where  represents a space)

print ('abc'),
print ('123'),
Copy after login

Related recommendations: "Python Video Tutorial"

2. Use from __future__import print_function to reference the method on Python 3.0. The following code will print: abc123 (without any extra characters)

Tip: After referencing this method, the method in 1 will become invalid. (But no grammatical error is reported, personal verification)

Remarks: from __future__ import print_function, __ is an underscore, that is, two shifts in the English input method _

from __future__ import print_function 
print ('abc',end='')
print ('123',end='')
Copy after login

Remarks: In fact, the print function The end parameter is set to '\n', so using the print function directly will automatically wrap the line.

And this method can customize the end symbol. The following code will print: abc&123&

from __future__ import print_function 
print ('abc',end='&')
print ('123',end='&')
Copy after login

3. Call the module sys. We will not discuss sys here for the time being. We only list the simple implementation code, as follows The code will print: abc123 (without any extra characters)

import sys
sys.stdout.write('abc')
sys.stdout.write('123')
Copy after login

The above is the detailed content of How to print without line breaks in python. 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