Home > Article > Backend Development > 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')
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: abc123 (where represents a space)
print ('abc'), print ('123'),
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='')
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='&')
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')
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!