Display character strings without line breaks in python (personal memo)

I wanted to display the character string without line breaks in python, so my memo

I was displaying a message for each process for progress management, but I was annoyed that the line breaks were made once.

test.py


for i in xrange(10):
    print "i = %d"%i

i=1 i=2 i=3 i=4

Like this. What I noticed here is an escape sequence called \ r. The return is represented by the reminiscent of an old typewriter. I think that this can be used

Since line breaks are not used, add a (comma) at the end of the sentence and add sleep so that you can check it.

test2.py


import time
for i in xrange(10):
    print "¥r i = %d"%i,
    time.sleep(0.3);

i=9

Nothing is displayed and only i = 9 is displayed. Java etc. worked well with this method, but python couldn't. Therefore, insert sys.stdout.flush () to force flush.

test3.py


import time
import sys
for i in xrange(10):
    print "¥r i = %d"%i,
    sys.stdout.flush()
    time.sleep(0.3)

Now it works fine.

There seems to be another way to use the standard output function of python3. Also, when I looked it up, there were many articles on this issue.

Recommended Posts

Display character strings without line breaks in python (personal memo)
Print with python3 without line breaks
[Introduction] Insert line breaks in Python 3
Insert a date without line breaks in CotEditor
Compare strings in Python
Reverse strings in Python
Output a character string with line breaks in PyYAML
[Python] Summary of eval / exec functions + How to write character strings with line breaks
3-3, Python strings and character codes
Fizzbuzz in Python (in one line)
Display UTM-30LX data in Python
[Line / Python] Beacon implementation memo
python> print> N line breaks> print ('\ n'* 3)> 4 line breaks> print ('\ n'* 3),> 3 line breaks
Search for strings in Python
Try LINE Notify in Python
Run LINE Bot implemented in Python (Flask) "without using Heroku"
Character code learned in Python
Make python segfault in one line
[Personal memo] Python virtual environment command memo
Web application development memo in python
Waveform display of audio in Python
Display characters like AA in python
Create Gmail in Python without API
Get, post communication memo in Python
Detect keystrokes in Python (without Enter)
[Beginner] Extract character strings with Python
[Personal memo] Python sequence type / mapping type
Separate display of Python graphs (memo)
I tried Line notification in Python
Implemented in 1 minute! LINE Notify in Python
Linux command (basic in basic) personal memo
Extract strings from files in Python
Display LaTeX notation formulas in Python, matplotlib
Make the line breaks visible in journalctrl
[Python, Julia] 3D display in Jupyter-Mayavi library
CGI server (1) python edition in one line
Python numbers, strings, list types (Python learning memo ①)
Display a list of alphabets in Python 3
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
Line graphs and scale lines in python
Display Python 3 in the browser with MAMP
Personal tips about Python (strings, around logs)
[Python beginner memo] Python character string, path operation
How to display multiplication table in python
Read the file line by line in Python
[Introduction to Udemy Python3 + Application] 11. Character strings
Read the file line by line in Python
Change line breaks in iPython autoformatted output
Bulk replacement of strings in Python arrays
Decompose command arguments in one line in Python
Display pyopengl in a browser (python + eel)
[Python] Invert bool value in one line
Image sending / receiving memo in Python (Flask)
CG image quality evaluation memo in Python
How to display Hello world in python
[Python] Read the specified line in the file
Personal best practices for putting command line launch Python projects in Docker containers
Character strings placed in GCS with python are garbled when viewed with a browser