Python: multiple prints on the same line





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







191















I want to run a script, which basicly shows things like:



Installing XXX...               [DONE]


Now, at the moment, I use print to print the whole line AFTER the function has succeeded. However, I now want it to print "Installing xxx..." first, and AFTER the function has run, to add the "DONE" tag; but on the same line.



Any ideas?










share|improve this question

























  • Possible duplicate of How do I keep Python print from adding newlines or spaces?

    – Cees Timmerman
    Dec 9 '16 at 2:21











  • Answers to this question don't mention that sometimes you want to clear the line, see: stackoverflow.com/questions/45263205

    – ideasman42
    Jul 23 '17 at 9:44


















191















I want to run a script, which basicly shows things like:



Installing XXX...               [DONE]


Now, at the moment, I use print to print the whole line AFTER the function has succeeded. However, I now want it to print "Installing xxx..." first, and AFTER the function has run, to add the "DONE" tag; but on the same line.



Any ideas?










share|improve this question

























  • Possible duplicate of How do I keep Python print from adding newlines or spaces?

    – Cees Timmerman
    Dec 9 '16 at 2:21











  • Answers to this question don't mention that sometimes you want to clear the line, see: stackoverflow.com/questions/45263205

    – ideasman42
    Jul 23 '17 at 9:44














191












191








191


35






I want to run a script, which basicly shows things like:



Installing XXX...               [DONE]


Now, at the moment, I use print to print the whole line AFTER the function has succeeded. However, I now want it to print "Installing xxx..." first, and AFTER the function has run, to add the "DONE" tag; but on the same line.



Any ideas?










share|improve this question
















I want to run a script, which basicly shows things like:



Installing XXX...               [DONE]


Now, at the moment, I use print to print the whole line AFTER the function has succeeded. However, I now want it to print "Installing xxx..." first, and AFTER the function has run, to add the "DONE" tag; but on the same line.



Any ideas?







python printing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 '18 at 21:57









Marco Bonelli

24k116475




24k116475










asked Apr 8 '11 at 16:38









user697108user697108

1,19631214




1,19631214













  • Possible duplicate of How do I keep Python print from adding newlines or spaces?

    – Cees Timmerman
    Dec 9 '16 at 2:21











  • Answers to this question don't mention that sometimes you want to clear the line, see: stackoverflow.com/questions/45263205

    – ideasman42
    Jul 23 '17 at 9:44



















  • Possible duplicate of How do I keep Python print from adding newlines or spaces?

    – Cees Timmerman
    Dec 9 '16 at 2:21











  • Answers to this question don't mention that sometimes you want to clear the line, see: stackoverflow.com/questions/45263205

    – ideasman42
    Jul 23 '17 at 9:44

















Possible duplicate of How do I keep Python print from adding newlines or spaces?

– Cees Timmerman
Dec 9 '16 at 2:21





Possible duplicate of How do I keep Python print from adding newlines or spaces?

– Cees Timmerman
Dec 9 '16 at 2:21













Answers to this question don't mention that sometimes you want to clear the line, see: stackoverflow.com/questions/45263205

– ideasman42
Jul 23 '17 at 9:44





Answers to this question don't mention that sometimes you want to clear the line, see: stackoverflow.com/questions/45263205

– ideasman42
Jul 23 '17 at 9:44












14 Answers
14






active

oldest

votes


















242














You can use the print statement to do this without importing sys.



def install_xxx():
print "Installing XXX... ",

install_xxx()
print "[DONE]"


The comma on the end of the print line prevents print from issuing a new line (you should note that there will be an extra space at the end of the output).



The Python 3 Solution

Since the above does not work in Python 3, you can do this instead (again, without importing sys):



def install_xxx():
print("Installing XXX... ", end="", flush=True)

install_xxx()
print("[DONE]")


The print function accepts an end parameter which defaults to "n". Setting it to an empty string prevents it from issuing a new line at the end of the line.






share|improve this answer





















  • 1





    It works perfectly. Had only seen stdout solutions so far. Really good to know that.

    – Prometheus
    Jan 8 '15 at 7:59






  • 4





    This doesn't work if you have both prints and a time consuming action in between (all in the same function / indentation level). Before the action starts, there is no output at all and after it is finished the output appears as whole

    – Paddre
    Feb 20 '15 at 15:08






  • 2





    That is probably more a function of the output buffering preformed by the OS for the process as a whole, which is not a python-specific problem. See stackoverflow.com/questions/107705 for a python-specific workaround.

    – multipleinterfaces
    Feb 20 '15 at 16:48








  • 11





    Without a newline, you probably want to explicitly flush the buffer. Use print("...", end="", flush=True) in Python 3, in Python 2 add a sys.stdout.flush() call.

    – Martijn Pieters
    Nov 3 '16 at 12:53






  • 2





    in python 3.x you'll want to add a "r" to end to replace the printed line VS appending to the end of it print("Progress: {}%".format(var), end="r", flush=True)

    – John
    Jan 6 '18 at 22:42



















99














You can simply use this:



print 'something',
...
print ' else',


and the output will be



something else


no need to overkill by import sys. Pay attention to comma symbol at the end.



Python 3+
print("some string", end=""); to remove the newline insert at the end. Read more by help(print);






share|improve this answer





















  • 15





    you should point out the comma

    – kommradHomer
    Jul 24 '13 at 10:25






  • 3





    Note there are two spaces by using this method

    – ethanjyx
    Nov 12 '13 at 3:20






  • 1





    This is the simplest and the most efficient answer.

    – gixxer
    May 22 '15 at 4:45











  • This does not work when arguments of print are in parentheses. This works: <print "hello",; print("hello")> but this doesnt work <print("hello",); print("hello")>

    – Mayank Jaiswal
    Nov 27 '15 at 7:46






  • 1





    print function has slightly different syntax in python 2 VS 3.

    – boldnik
    Feb 7 '16 at 4:24



















42














Use sys.stdout.write('Installing XXX... ') and sys.stdout.write('Done'). In this way, you have to add the new line by hand with "n" if you want to recreate the print functionality. I think that it might be unnecessary to use curses just for this.






share|improve this answer





















  • 1





    Thanks! This works fine.

    – user697108
    Apr 8 '11 at 16:50






  • 4





    I personally prefer this solution to the higher voted one because it works the exact same way on python2.x and python3.x without needing to rely on __future__ imports or anything like that.

    – mgilson
    Jan 31 '13 at 14:29



















41














You should use backspace 'r' or ('x08') char to go back on previous position in console output



Python 2+:



import time
import sys

def backspace(n):
sys.stdout.write((b'x08' * n).decode()) # use x08 char to go back

for i in range(101): # for 0 to 100
s = str(i) + '%' # string for output
sys.stdout.write(s) # just print
sys.stdout.flush() # needed for flush when using x08
backspace(len(s)) # back n chars
time.sleep(0.2) # sleep for 200ms


Python 3:



import time   

def backline():
print('r', end='') # use 'r' to go back


for i in range(101): # for 0 to 100
s = str(i) + '%' # string for output
print(s, end='') # just print and flush
backline() # back to the beginning of line
time.sleep(0.2) # sleep for 200ms


This code will count from 0% to 100% on one line. Final value will be:



> python test.py
100%


Additional info about flush in this case here: Why do python print statements that contain 'end=' arguments behave differently in while-loops?






share|improve this answer





















  • 1





    To clarify, it looks like the commented-out code in this example is for Python 2, and the non-commented lines are for Python 3.

    – Turtles Are Cute
    Mar 9 '15 at 23:05






  • 1





    bouth lines will work fine in Python 3. If you use 'x08' as a backspace you need to flush the output stream - print((b'x08' * n).decode(), end='', flush=True)

    – Vadim Zin4uk
    Mar 10 '15 at 9:43








  • 1





    no chance with python 2.7 on linux. :(

    – obayhan
    Nov 5 '15 at 13:08






  • 1





    The carriage return 'r' goes back to the beginning of the line, so the '* n' is unnecessary

    – bjnortier
    May 8 '17 at 8:02













  • @bjnortier, right, corrected

    – ideasman42
    Jul 23 '17 at 8:33



















16














None of the answers worked for me since they all paused until a new line was encountered. I wrote a simple helper:



def print_no_newline(string):
import sys
sys.stdout.write(string)
sys.stdout.flush()


To test it:



import time
print_no_newline('hello ')
# Simulate a long task
time.sleep(2)
print('world')


"hello " will first print out and flush to the screen before the sleep. After that you can use standard print.






share|improve this answer



















  • 1





    Thank you! Exactly what i needed aka .flush()

    – MrNice
    Oct 20 '16 at 12:28



















14














sys.stdout.write will print without return carriage



import sys
sys.stdout.write("installing xxx")
sys.stdout.write(".")


http://en.wikibooks.org/wiki/Python_Programming/Input_and_output#printing_without_commas_or_newlines






share|improve this answer































    7














    This simple example will print 1-10 on the same line.



    for i in range(1,11):
    print (i, end=" ")





    share|improve this answer































      6














      Print has an optional end argument, it is what printed in the end.
      The default is newline but you can change it to empty string. eg: print("hello world!", end="")






      share|improve this answer
























      • it gives a syntax error

        – thedp
        Jul 13 '15 at 14:37






      • 2





        This is in python 3. In python 2 you can simply print 'something',. The comma at the end prevents the addition of a newline.

        – arjoonn
        Sep 6 '15 at 5:37





















      6














      Most simple:



      Python 3



          print(‘r’+’something to be override’,end=‘’)


      It means it will back the cursor to beginning, than will print something and will end in the same line. If in a loop it will start printing in the same place it starts.






      share|improve this answer


























      • Cool! Works in Windows as well.

        – Shital Shah
        Feb 13 at 11:59











      • Thanks this works fine for me! I think that print(’something to be override’, end=‘r’) is simpler, though.

        – Francesco Boccardo
        Apr 10 at 9:00



















      4














      If you want to overwrite the previous line (rather than continually adding to it), you can combine r with print(), at the end of the print statement. For example,



      from time import sleep

      for i in xrange(0, 10):
      print("r{0}".format(i)),
      sleep(.5)

      print("...DONE!")


      will count 0 to 9, replacing the old number in the console. The "...DONE!" will print on the same line as the last counter, 9.



      In your case for the OP, this would allow the console to display percent complete of the install as a "progress bar", where you can define a begin and end character position, and update the markers in between.



      print("Installing |XXXXXX              | 30%"),





      share|improve this answer

































        3














        Here a 2.7-compatible version derived from the 3.0 version by @Vadim-Zin4uk:



        Python 2



        import time

        for i in range(101): # for 0 to 100
        s = str(i) + '%' # string for output
        print '{0}r'.format(s), # just print and flush

        time.sleep(0.2)


        For that matter, the 3.0 solution provided looks a little bloated. For example, the backspace method doesn't make use of the integer argument and could probably be done away with altogether.



        Python 3



        import time

        for i in range(101): # for 0 to 100
        s = str(i) + '%' # string for output
        print('{0}r'.format(s), end='') # just print and flush

        time.sleep(0.2) # sleep for 200ms


        Both have been tested and work.






        share|improve this answer































          2














          This is a very old thread, but here's a very thorough answer and sample code.



          r is the string representation of Carriage Return from the ASCII character set. It's the same as octal 015 [chr(0o15)] or hexidecimal 0d [chr(0x0d)] or decimal 13 [chr(13)]. See man ascii for a boring read. It (r) is a pretty portable representation and is easy enough for people to read. It very simply means to move the carriage on the typewriter all the way back to the start without advancing the paper. It's the CR part of CRLF which means Carriage Return and Line Feed.



          print() is a function in Python 3. In Python 2 (any version that you'd be interested in using), print can be forced into a function by importing its definition from the __future__ module. The benefit of the print function is that you can specify what to print at the end, overriding the default behavior of n to print a newline at the end of every print() call.



          sys.stdout.flush tells Python to flush the output of standard output, which is where you send output with print() unless you specify otherwise. You can also get the same behavior by running with python -u or setting environment variable PYTHONUNBUFFERED=1, thereby skipping the import sys and sys.stdout.flush() calls. The amount you gain by doing that is almost exactly zero and isn't very easy to debug if you conveniently forget that you have to do that step before your application behaves properly.



          And a sample. Note that this runs perfectly in Python 2 or 3.



          from __future__ import print_function

          import sys
          import time

          ANS = 42
          FACTORS = {n for n in range(1, ANS + 1) if ANS % n == 0}

          for i in range(1, ANS + 1):
          if i in FACTORS:
          print('r{0:d}'.format(i), end='')
          sys.stdout.flush()
          time.sleep(ANS / 100.0)
          else:
          print()





          share|improve this answer
























          • I think your answer is more suitable for: stackoverflow.com/questions/45263205/…

            – Marc Cayuela Rafols
            Oct 25 '17 at 19:06



















          1














          Just in case you have pre-stored the values in an array, you can call them in the following format:



          for i in range(0,n):
          print arr[i],





          share|improve this answer































            -3














            I found this solution, and it's working on Python 2.7



            # Working on Python 2.7 Linux

            import time
            import sys


            def backspace(n):
            print('r', end='') # use 'r' to go back


            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            sys.stdout.write(string)
            backspace(len(s)) # back for n chars
            sys.stdout.flush()
            time.sleep(0.2) # sleep for 200ms





            share|improve this answer





















            • 1





              That print is incorrect syntax for Python 2.7 and the code doesn't work even with correct syntax.

              – Cory Madden
              Aug 6 '17 at 20:47












            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5598181%2fpython-multiple-prints-on-the-same-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            14 Answers
            14






            active

            oldest

            votes








            14 Answers
            14






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            242














            You can use the print statement to do this without importing sys.



            def install_xxx():
            print "Installing XXX... ",

            install_xxx()
            print "[DONE]"


            The comma on the end of the print line prevents print from issuing a new line (you should note that there will be an extra space at the end of the output).



            The Python 3 Solution

            Since the above does not work in Python 3, you can do this instead (again, without importing sys):



            def install_xxx():
            print("Installing XXX... ", end="", flush=True)

            install_xxx()
            print("[DONE]")


            The print function accepts an end parameter which defaults to "n". Setting it to an empty string prevents it from issuing a new line at the end of the line.






            share|improve this answer





















            • 1





              It works perfectly. Had only seen stdout solutions so far. Really good to know that.

              – Prometheus
              Jan 8 '15 at 7:59






            • 4





              This doesn't work if you have both prints and a time consuming action in between (all in the same function / indentation level). Before the action starts, there is no output at all and after it is finished the output appears as whole

              – Paddre
              Feb 20 '15 at 15:08






            • 2





              That is probably more a function of the output buffering preformed by the OS for the process as a whole, which is not a python-specific problem. See stackoverflow.com/questions/107705 for a python-specific workaround.

              – multipleinterfaces
              Feb 20 '15 at 16:48








            • 11





              Without a newline, you probably want to explicitly flush the buffer. Use print("...", end="", flush=True) in Python 3, in Python 2 add a sys.stdout.flush() call.

              – Martijn Pieters
              Nov 3 '16 at 12:53






            • 2





              in python 3.x you'll want to add a "r" to end to replace the printed line VS appending to the end of it print("Progress: {}%".format(var), end="r", flush=True)

              – John
              Jan 6 '18 at 22:42
















            242














            You can use the print statement to do this without importing sys.



            def install_xxx():
            print "Installing XXX... ",

            install_xxx()
            print "[DONE]"


            The comma on the end of the print line prevents print from issuing a new line (you should note that there will be an extra space at the end of the output).



            The Python 3 Solution

            Since the above does not work in Python 3, you can do this instead (again, without importing sys):



            def install_xxx():
            print("Installing XXX... ", end="", flush=True)

            install_xxx()
            print("[DONE]")


            The print function accepts an end parameter which defaults to "n". Setting it to an empty string prevents it from issuing a new line at the end of the line.






            share|improve this answer





















            • 1





              It works perfectly. Had only seen stdout solutions so far. Really good to know that.

              – Prometheus
              Jan 8 '15 at 7:59






            • 4





              This doesn't work if you have both prints and a time consuming action in between (all in the same function / indentation level). Before the action starts, there is no output at all and after it is finished the output appears as whole

              – Paddre
              Feb 20 '15 at 15:08






            • 2





              That is probably more a function of the output buffering preformed by the OS for the process as a whole, which is not a python-specific problem. See stackoverflow.com/questions/107705 for a python-specific workaround.

              – multipleinterfaces
              Feb 20 '15 at 16:48








            • 11





              Without a newline, you probably want to explicitly flush the buffer. Use print("...", end="", flush=True) in Python 3, in Python 2 add a sys.stdout.flush() call.

              – Martijn Pieters
              Nov 3 '16 at 12:53






            • 2





              in python 3.x you'll want to add a "r" to end to replace the printed line VS appending to the end of it print("Progress: {}%".format(var), end="r", flush=True)

              – John
              Jan 6 '18 at 22:42














            242












            242








            242







            You can use the print statement to do this without importing sys.



            def install_xxx():
            print "Installing XXX... ",

            install_xxx()
            print "[DONE]"


            The comma on the end of the print line prevents print from issuing a new line (you should note that there will be an extra space at the end of the output).



            The Python 3 Solution

            Since the above does not work in Python 3, you can do this instead (again, without importing sys):



            def install_xxx():
            print("Installing XXX... ", end="", flush=True)

            install_xxx()
            print("[DONE]")


            The print function accepts an end parameter which defaults to "n". Setting it to an empty string prevents it from issuing a new line at the end of the line.






            share|improve this answer















            You can use the print statement to do this without importing sys.



            def install_xxx():
            print "Installing XXX... ",

            install_xxx()
            print "[DONE]"


            The comma on the end of the print line prevents print from issuing a new line (you should note that there will be an extra space at the end of the output).



            The Python 3 Solution

            Since the above does not work in Python 3, you can do this instead (again, without importing sys):



            def install_xxx():
            print("Installing XXX... ", end="", flush=True)

            install_xxx()
            print("[DONE]")


            The print function accepts an end parameter which defaults to "n". Setting it to an empty string prevents it from issuing a new line at the end of the line.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 7 '18 at 21:57









            Marco Bonelli

            24k116475




            24k116475










            answered Apr 8 '11 at 16:56









            multipleinterfacesmultipleinterfaces

            6,55642331




            6,55642331








            • 1





              It works perfectly. Had only seen stdout solutions so far. Really good to know that.

              – Prometheus
              Jan 8 '15 at 7:59






            • 4





              This doesn't work if you have both prints and a time consuming action in between (all in the same function / indentation level). Before the action starts, there is no output at all and after it is finished the output appears as whole

              – Paddre
              Feb 20 '15 at 15:08






            • 2





              That is probably more a function of the output buffering preformed by the OS for the process as a whole, which is not a python-specific problem. See stackoverflow.com/questions/107705 for a python-specific workaround.

              – multipleinterfaces
              Feb 20 '15 at 16:48








            • 11





              Without a newline, you probably want to explicitly flush the buffer. Use print("...", end="", flush=True) in Python 3, in Python 2 add a sys.stdout.flush() call.

              – Martijn Pieters
              Nov 3 '16 at 12:53






            • 2





              in python 3.x you'll want to add a "r" to end to replace the printed line VS appending to the end of it print("Progress: {}%".format(var), end="r", flush=True)

              – John
              Jan 6 '18 at 22:42














            • 1





              It works perfectly. Had only seen stdout solutions so far. Really good to know that.

              – Prometheus
              Jan 8 '15 at 7:59






            • 4





              This doesn't work if you have both prints and a time consuming action in between (all in the same function / indentation level). Before the action starts, there is no output at all and after it is finished the output appears as whole

              – Paddre
              Feb 20 '15 at 15:08






            • 2





              That is probably more a function of the output buffering preformed by the OS for the process as a whole, which is not a python-specific problem. See stackoverflow.com/questions/107705 for a python-specific workaround.

              – multipleinterfaces
              Feb 20 '15 at 16:48








            • 11





              Without a newline, you probably want to explicitly flush the buffer. Use print("...", end="", flush=True) in Python 3, in Python 2 add a sys.stdout.flush() call.

              – Martijn Pieters
              Nov 3 '16 at 12:53






            • 2





              in python 3.x you'll want to add a "r" to end to replace the printed line VS appending to the end of it print("Progress: {}%".format(var), end="r", flush=True)

              – John
              Jan 6 '18 at 22:42








            1




            1





            It works perfectly. Had only seen stdout solutions so far. Really good to know that.

            – Prometheus
            Jan 8 '15 at 7:59





            It works perfectly. Had only seen stdout solutions so far. Really good to know that.

            – Prometheus
            Jan 8 '15 at 7:59




            4




            4





            This doesn't work if you have both prints and a time consuming action in between (all in the same function / indentation level). Before the action starts, there is no output at all and after it is finished the output appears as whole

            – Paddre
            Feb 20 '15 at 15:08





            This doesn't work if you have both prints and a time consuming action in between (all in the same function / indentation level). Before the action starts, there is no output at all and after it is finished the output appears as whole

            – Paddre
            Feb 20 '15 at 15:08




            2




            2





            That is probably more a function of the output buffering preformed by the OS for the process as a whole, which is not a python-specific problem. See stackoverflow.com/questions/107705 for a python-specific workaround.

            – multipleinterfaces
            Feb 20 '15 at 16:48







            That is probably more a function of the output buffering preformed by the OS for the process as a whole, which is not a python-specific problem. See stackoverflow.com/questions/107705 for a python-specific workaround.

            – multipleinterfaces
            Feb 20 '15 at 16:48






            11




            11





            Without a newline, you probably want to explicitly flush the buffer. Use print("...", end="", flush=True) in Python 3, in Python 2 add a sys.stdout.flush() call.

            – Martijn Pieters
            Nov 3 '16 at 12:53





            Without a newline, you probably want to explicitly flush the buffer. Use print("...", end="", flush=True) in Python 3, in Python 2 add a sys.stdout.flush() call.

            – Martijn Pieters
            Nov 3 '16 at 12:53




            2




            2





            in python 3.x you'll want to add a "r" to end to replace the printed line VS appending to the end of it print("Progress: {}%".format(var), end="r", flush=True)

            – John
            Jan 6 '18 at 22:42





            in python 3.x you'll want to add a "r" to end to replace the printed line VS appending to the end of it print("Progress: {}%".format(var), end="r", flush=True)

            – John
            Jan 6 '18 at 22:42













            99














            You can simply use this:



            print 'something',
            ...
            print ' else',


            and the output will be



            something else


            no need to overkill by import sys. Pay attention to comma symbol at the end.



            Python 3+
            print("some string", end=""); to remove the newline insert at the end. Read more by help(print);






            share|improve this answer





















            • 15





              you should point out the comma

              – kommradHomer
              Jul 24 '13 at 10:25






            • 3





              Note there are two spaces by using this method

              – ethanjyx
              Nov 12 '13 at 3:20






            • 1





              This is the simplest and the most efficient answer.

              – gixxer
              May 22 '15 at 4:45











            • This does not work when arguments of print are in parentheses. This works: <print "hello",; print("hello")> but this doesnt work <print("hello",); print("hello")>

              – Mayank Jaiswal
              Nov 27 '15 at 7:46






            • 1





              print function has slightly different syntax in python 2 VS 3.

              – boldnik
              Feb 7 '16 at 4:24
















            99














            You can simply use this:



            print 'something',
            ...
            print ' else',


            and the output will be



            something else


            no need to overkill by import sys. Pay attention to comma symbol at the end.



            Python 3+
            print("some string", end=""); to remove the newline insert at the end. Read more by help(print);






            share|improve this answer





















            • 15





              you should point out the comma

              – kommradHomer
              Jul 24 '13 at 10:25






            • 3





              Note there are two spaces by using this method

              – ethanjyx
              Nov 12 '13 at 3:20






            • 1





              This is the simplest and the most efficient answer.

              – gixxer
              May 22 '15 at 4:45











            • This does not work when arguments of print are in parentheses. This works: <print "hello",; print("hello")> but this doesnt work <print("hello",); print("hello")>

              – Mayank Jaiswal
              Nov 27 '15 at 7:46






            • 1





              print function has slightly different syntax in python 2 VS 3.

              – boldnik
              Feb 7 '16 at 4:24














            99












            99








            99







            You can simply use this:



            print 'something',
            ...
            print ' else',


            and the output will be



            something else


            no need to overkill by import sys. Pay attention to comma symbol at the end.



            Python 3+
            print("some string", end=""); to remove the newline insert at the end. Read more by help(print);






            share|improve this answer















            You can simply use this:



            print 'something',
            ...
            print ' else',


            and the output will be



            something else


            no need to overkill by import sys. Pay attention to comma symbol at the end.



            Python 3+
            print("some string", end=""); to remove the newline insert at the end. Read more by help(print);







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 4 '17 at 11:41

























            answered Mar 14 '13 at 13:35









            boldnikboldnik

            1,69211422




            1,69211422








            • 15





              you should point out the comma

              – kommradHomer
              Jul 24 '13 at 10:25






            • 3





              Note there are two spaces by using this method

              – ethanjyx
              Nov 12 '13 at 3:20






            • 1





              This is the simplest and the most efficient answer.

              – gixxer
              May 22 '15 at 4:45











            • This does not work when arguments of print are in parentheses. This works: <print "hello",; print("hello")> but this doesnt work <print("hello",); print("hello")>

              – Mayank Jaiswal
              Nov 27 '15 at 7:46






            • 1





              print function has slightly different syntax in python 2 VS 3.

              – boldnik
              Feb 7 '16 at 4:24














            • 15





              you should point out the comma

              – kommradHomer
              Jul 24 '13 at 10:25






            • 3





              Note there are two spaces by using this method

              – ethanjyx
              Nov 12 '13 at 3:20






            • 1





              This is the simplest and the most efficient answer.

              – gixxer
              May 22 '15 at 4:45











            • This does not work when arguments of print are in parentheses. This works: <print "hello",; print("hello")> but this doesnt work <print("hello",); print("hello")>

              – Mayank Jaiswal
              Nov 27 '15 at 7:46






            • 1





              print function has slightly different syntax in python 2 VS 3.

              – boldnik
              Feb 7 '16 at 4:24








            15




            15





            you should point out the comma

            – kommradHomer
            Jul 24 '13 at 10:25





            you should point out the comma

            – kommradHomer
            Jul 24 '13 at 10:25




            3




            3





            Note there are two spaces by using this method

            – ethanjyx
            Nov 12 '13 at 3:20





            Note there are two spaces by using this method

            – ethanjyx
            Nov 12 '13 at 3:20




            1




            1





            This is the simplest and the most efficient answer.

            – gixxer
            May 22 '15 at 4:45





            This is the simplest and the most efficient answer.

            – gixxer
            May 22 '15 at 4:45













            This does not work when arguments of print are in parentheses. This works: <print "hello",; print("hello")> but this doesnt work <print("hello",); print("hello")>

            – Mayank Jaiswal
            Nov 27 '15 at 7:46





            This does not work when arguments of print are in parentheses. This works: <print "hello",; print("hello")> but this doesnt work <print("hello",); print("hello")>

            – Mayank Jaiswal
            Nov 27 '15 at 7:46




            1




            1





            print function has slightly different syntax in python 2 VS 3.

            – boldnik
            Feb 7 '16 at 4:24





            print function has slightly different syntax in python 2 VS 3.

            – boldnik
            Feb 7 '16 at 4:24











            42














            Use sys.stdout.write('Installing XXX... ') and sys.stdout.write('Done'). In this way, you have to add the new line by hand with "n" if you want to recreate the print functionality. I think that it might be unnecessary to use curses just for this.






            share|improve this answer





















            • 1





              Thanks! This works fine.

              – user697108
              Apr 8 '11 at 16:50






            • 4





              I personally prefer this solution to the higher voted one because it works the exact same way on python2.x and python3.x without needing to rely on __future__ imports or anything like that.

              – mgilson
              Jan 31 '13 at 14:29
















            42














            Use sys.stdout.write('Installing XXX... ') and sys.stdout.write('Done'). In this way, you have to add the new line by hand with "n" if you want to recreate the print functionality. I think that it might be unnecessary to use curses just for this.






            share|improve this answer





















            • 1





              Thanks! This works fine.

              – user697108
              Apr 8 '11 at 16:50






            • 4





              I personally prefer this solution to the higher voted one because it works the exact same way on python2.x and python3.x without needing to rely on __future__ imports or anything like that.

              – mgilson
              Jan 31 '13 at 14:29














            42












            42








            42







            Use sys.stdout.write('Installing XXX... ') and sys.stdout.write('Done'). In this way, you have to add the new line by hand with "n" if you want to recreate the print functionality. I think that it might be unnecessary to use curses just for this.






            share|improve this answer















            Use sys.stdout.write('Installing XXX... ') and sys.stdout.write('Done'). In this way, you have to add the new line by hand with "n" if you want to recreate the print functionality. I think that it might be unnecessary to use curses just for this.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 31 '13 at 14:27









            mgilson

            214k40424535




            214k40424535










            answered Apr 8 '11 at 16:42









            ferostarferostar

            6,02963359




            6,02963359








            • 1





              Thanks! This works fine.

              – user697108
              Apr 8 '11 at 16:50






            • 4





              I personally prefer this solution to the higher voted one because it works the exact same way on python2.x and python3.x without needing to rely on __future__ imports or anything like that.

              – mgilson
              Jan 31 '13 at 14:29














            • 1





              Thanks! This works fine.

              – user697108
              Apr 8 '11 at 16:50






            • 4





              I personally prefer this solution to the higher voted one because it works the exact same way on python2.x and python3.x without needing to rely on __future__ imports or anything like that.

              – mgilson
              Jan 31 '13 at 14:29








            1




            1





            Thanks! This works fine.

            – user697108
            Apr 8 '11 at 16:50





            Thanks! This works fine.

            – user697108
            Apr 8 '11 at 16:50




            4




            4





            I personally prefer this solution to the higher voted one because it works the exact same way on python2.x and python3.x without needing to rely on __future__ imports or anything like that.

            – mgilson
            Jan 31 '13 at 14:29





            I personally prefer this solution to the higher voted one because it works the exact same way on python2.x and python3.x without needing to rely on __future__ imports or anything like that.

            – mgilson
            Jan 31 '13 at 14:29











            41














            You should use backspace 'r' or ('x08') char to go back on previous position in console output



            Python 2+:



            import time
            import sys

            def backspace(n):
            sys.stdout.write((b'x08' * n).decode()) # use x08 char to go back

            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            sys.stdout.write(s) # just print
            sys.stdout.flush() # needed for flush when using x08
            backspace(len(s)) # back n chars
            time.sleep(0.2) # sleep for 200ms


            Python 3:



            import time   

            def backline():
            print('r', end='') # use 'r' to go back


            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            print(s, end='') # just print and flush
            backline() # back to the beginning of line
            time.sleep(0.2) # sleep for 200ms


            This code will count from 0% to 100% on one line. Final value will be:



            > python test.py
            100%


            Additional info about flush in this case here: Why do python print statements that contain 'end=' arguments behave differently in while-loops?






            share|improve this answer





















            • 1





              To clarify, it looks like the commented-out code in this example is for Python 2, and the non-commented lines are for Python 3.

              – Turtles Are Cute
              Mar 9 '15 at 23:05






            • 1





              bouth lines will work fine in Python 3. If you use 'x08' as a backspace you need to flush the output stream - print((b'x08' * n).decode(), end='', flush=True)

              – Vadim Zin4uk
              Mar 10 '15 at 9:43








            • 1





              no chance with python 2.7 on linux. :(

              – obayhan
              Nov 5 '15 at 13:08






            • 1





              The carriage return 'r' goes back to the beginning of the line, so the '* n' is unnecessary

              – bjnortier
              May 8 '17 at 8:02













            • @bjnortier, right, corrected

              – ideasman42
              Jul 23 '17 at 8:33
















            41














            You should use backspace 'r' or ('x08') char to go back on previous position in console output



            Python 2+:



            import time
            import sys

            def backspace(n):
            sys.stdout.write((b'x08' * n).decode()) # use x08 char to go back

            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            sys.stdout.write(s) # just print
            sys.stdout.flush() # needed for flush when using x08
            backspace(len(s)) # back n chars
            time.sleep(0.2) # sleep for 200ms


            Python 3:



            import time   

            def backline():
            print('r', end='') # use 'r' to go back


            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            print(s, end='') # just print and flush
            backline() # back to the beginning of line
            time.sleep(0.2) # sleep for 200ms


            This code will count from 0% to 100% on one line. Final value will be:



            > python test.py
            100%


            Additional info about flush in this case here: Why do python print statements that contain 'end=' arguments behave differently in while-loops?






            share|improve this answer





















            • 1





              To clarify, it looks like the commented-out code in this example is for Python 2, and the non-commented lines are for Python 3.

              – Turtles Are Cute
              Mar 9 '15 at 23:05






            • 1





              bouth lines will work fine in Python 3. If you use 'x08' as a backspace you need to flush the output stream - print((b'x08' * n).decode(), end='', flush=True)

              – Vadim Zin4uk
              Mar 10 '15 at 9:43








            • 1





              no chance with python 2.7 on linux. :(

              – obayhan
              Nov 5 '15 at 13:08






            • 1





              The carriage return 'r' goes back to the beginning of the line, so the '* n' is unnecessary

              – bjnortier
              May 8 '17 at 8:02













            • @bjnortier, right, corrected

              – ideasman42
              Jul 23 '17 at 8:33














            41












            41








            41







            You should use backspace 'r' or ('x08') char to go back on previous position in console output



            Python 2+:



            import time
            import sys

            def backspace(n):
            sys.stdout.write((b'x08' * n).decode()) # use x08 char to go back

            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            sys.stdout.write(s) # just print
            sys.stdout.flush() # needed for flush when using x08
            backspace(len(s)) # back n chars
            time.sleep(0.2) # sleep for 200ms


            Python 3:



            import time   

            def backline():
            print('r', end='') # use 'r' to go back


            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            print(s, end='') # just print and flush
            backline() # back to the beginning of line
            time.sleep(0.2) # sleep for 200ms


            This code will count from 0% to 100% on one line. Final value will be:



            > python test.py
            100%


            Additional info about flush in this case here: Why do python print statements that contain 'end=' arguments behave differently in while-loops?






            share|improve this answer















            You should use backspace 'r' or ('x08') char to go back on previous position in console output



            Python 2+:



            import time
            import sys

            def backspace(n):
            sys.stdout.write((b'x08' * n).decode()) # use x08 char to go back

            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            sys.stdout.write(s) # just print
            sys.stdout.flush() # needed for flush when using x08
            backspace(len(s)) # back n chars
            time.sleep(0.2) # sleep for 200ms


            Python 3:



            import time   

            def backline():
            print('r', end='') # use 'r' to go back


            for i in range(101): # for 0 to 100
            s = str(i) + '%' # string for output
            print(s, end='') # just print and flush
            backline() # back to the beginning of line
            time.sleep(0.2) # sleep for 200ms


            This code will count from 0% to 100% on one line. Final value will be:



            > python test.py
            100%


            Additional info about flush in this case here: Why do python print statements that contain 'end=' arguments behave differently in while-loops?







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 10 at 9:28









            Francesco Boccardo

            632220




            632220










            answered Mar 31 '14 at 8:57









            Vadim Zin4ukVadim Zin4uk

            1,2051513




            1,2051513








            • 1





              To clarify, it looks like the commented-out code in this example is for Python 2, and the non-commented lines are for Python 3.

              – Turtles Are Cute
              Mar 9 '15 at 23:05






            • 1





              bouth lines will work fine in Python 3. If you use 'x08' as a backspace you need to flush the output stream - print((b'x08' * n).decode(), end='', flush=True)

              – Vadim Zin4uk
              Mar 10 '15 at 9:43








            • 1





              no chance with python 2.7 on linux. :(

              – obayhan
              Nov 5 '15 at 13:08






            • 1





              The carriage return 'r' goes back to the beginning of the line, so the '* n' is unnecessary

              – bjnortier
              May 8 '17 at 8:02













            • @bjnortier, right, corrected

              – ideasman42
              Jul 23 '17 at 8:33














            • 1





              To clarify, it looks like the commented-out code in this example is for Python 2, and the non-commented lines are for Python 3.

              – Turtles Are Cute
              Mar 9 '15 at 23:05






            • 1





              bouth lines will work fine in Python 3. If you use 'x08' as a backspace you need to flush the output stream - print((b'x08' * n).decode(), end='', flush=True)

              – Vadim Zin4uk
              Mar 10 '15 at 9:43








            • 1





              no chance with python 2.7 on linux. :(

              – obayhan
              Nov 5 '15 at 13:08






            • 1





              The carriage return 'r' goes back to the beginning of the line, so the '* n' is unnecessary

              – bjnortier
              May 8 '17 at 8:02













            • @bjnortier, right, corrected

              – ideasman42
              Jul 23 '17 at 8:33








            1




            1





            To clarify, it looks like the commented-out code in this example is for Python 2, and the non-commented lines are for Python 3.

            – Turtles Are Cute
            Mar 9 '15 at 23:05





            To clarify, it looks like the commented-out code in this example is for Python 2, and the non-commented lines are for Python 3.

            – Turtles Are Cute
            Mar 9 '15 at 23:05




            1




            1





            bouth lines will work fine in Python 3. If you use 'x08' as a backspace you need to flush the output stream - print((b'x08' * n).decode(), end='', flush=True)

            – Vadim Zin4uk
            Mar 10 '15 at 9:43







            bouth lines will work fine in Python 3. If you use 'x08' as a backspace you need to flush the output stream - print((b'x08' * n).decode(), end='', flush=True)

            – Vadim Zin4uk
            Mar 10 '15 at 9:43






            1




            1





            no chance with python 2.7 on linux. :(

            – obayhan
            Nov 5 '15 at 13:08





            no chance with python 2.7 on linux. :(

            – obayhan
            Nov 5 '15 at 13:08




            1




            1





            The carriage return 'r' goes back to the beginning of the line, so the '* n' is unnecessary

            – bjnortier
            May 8 '17 at 8:02







            The carriage return 'r' goes back to the beginning of the line, so the '* n' is unnecessary

            – bjnortier
            May 8 '17 at 8:02















            @bjnortier, right, corrected

            – ideasman42
            Jul 23 '17 at 8:33





            @bjnortier, right, corrected

            – ideasman42
            Jul 23 '17 at 8:33











            16














            None of the answers worked for me since they all paused until a new line was encountered. I wrote a simple helper:



            def print_no_newline(string):
            import sys
            sys.stdout.write(string)
            sys.stdout.flush()


            To test it:



            import time
            print_no_newline('hello ')
            # Simulate a long task
            time.sleep(2)
            print('world')


            "hello " will first print out and flush to the screen before the sleep. After that you can use standard print.






            share|improve this answer



















            • 1





              Thank you! Exactly what i needed aka .flush()

              – MrNice
              Oct 20 '16 at 12:28
















            16














            None of the answers worked for me since they all paused until a new line was encountered. I wrote a simple helper:



            def print_no_newline(string):
            import sys
            sys.stdout.write(string)
            sys.stdout.flush()


            To test it:



            import time
            print_no_newline('hello ')
            # Simulate a long task
            time.sleep(2)
            print('world')


            "hello " will first print out and flush to the screen before the sleep. After that you can use standard print.






            share|improve this answer



















            • 1





              Thank you! Exactly what i needed aka .flush()

              – MrNice
              Oct 20 '16 at 12:28














            16












            16








            16







            None of the answers worked for me since they all paused until a new line was encountered. I wrote a simple helper:



            def print_no_newline(string):
            import sys
            sys.stdout.write(string)
            sys.stdout.flush()


            To test it:



            import time
            print_no_newline('hello ')
            # Simulate a long task
            time.sleep(2)
            print('world')


            "hello " will first print out and flush to the screen before the sleep. After that you can use standard print.






            share|improve this answer













            None of the answers worked for me since they all paused until a new line was encountered. I wrote a simple helper:



            def print_no_newline(string):
            import sys
            sys.stdout.write(string)
            sys.stdout.flush()


            To test it:



            import time
            print_no_newline('hello ')
            # Simulate a long task
            time.sleep(2)
            print('world')


            "hello " will first print out and flush to the screen before the sleep. After that you can use standard print.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 22 '13 at 18:23









            incognickincognick

            1,69821514




            1,69821514








            • 1





              Thank you! Exactly what i needed aka .flush()

              – MrNice
              Oct 20 '16 at 12:28














            • 1





              Thank you! Exactly what i needed aka .flush()

              – MrNice
              Oct 20 '16 at 12:28








            1




            1





            Thank you! Exactly what i needed aka .flush()

            – MrNice
            Oct 20 '16 at 12:28





            Thank you! Exactly what i needed aka .flush()

            – MrNice
            Oct 20 '16 at 12:28











            14














            sys.stdout.write will print without return carriage



            import sys
            sys.stdout.write("installing xxx")
            sys.stdout.write(".")


            http://en.wikibooks.org/wiki/Python_Programming/Input_and_output#printing_without_commas_or_newlines






            share|improve this answer




























              14














              sys.stdout.write will print without return carriage



              import sys
              sys.stdout.write("installing xxx")
              sys.stdout.write(".")


              http://en.wikibooks.org/wiki/Python_Programming/Input_and_output#printing_without_commas_or_newlines






              share|improve this answer


























                14












                14








                14







                sys.stdout.write will print without return carriage



                import sys
                sys.stdout.write("installing xxx")
                sys.stdout.write(".")


                http://en.wikibooks.org/wiki/Python_Programming/Input_and_output#printing_without_commas_or_newlines






                share|improve this answer













                sys.stdout.write will print without return carriage



                import sys
                sys.stdout.write("installing xxx")
                sys.stdout.write(".")


                http://en.wikibooks.org/wiki/Python_Programming/Input_and_output#printing_without_commas_or_newlines







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 8 '11 at 16:43









                John GiottaJohn Giotta

                12.4k43871




                12.4k43871























                    7














                    This simple example will print 1-10 on the same line.



                    for i in range(1,11):
                    print (i, end=" ")





                    share|improve this answer




























                      7














                      This simple example will print 1-10 on the same line.



                      for i in range(1,11):
                      print (i, end=" ")





                      share|improve this answer


























                        7












                        7








                        7







                        This simple example will print 1-10 on the same line.



                        for i in range(1,11):
                        print (i, end=" ")





                        share|improve this answer













                        This simple example will print 1-10 on the same line.



                        for i in range(1,11):
                        print (i, end=" ")






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered May 15 '18 at 7:01









                        Umali LeonardUmali Leonard

                        7111




                        7111























                            6














                            Print has an optional end argument, it is what printed in the end.
                            The default is newline but you can change it to empty string. eg: print("hello world!", end="")






                            share|improve this answer
























                            • it gives a syntax error

                              – thedp
                              Jul 13 '15 at 14:37






                            • 2





                              This is in python 3. In python 2 you can simply print 'something',. The comma at the end prevents the addition of a newline.

                              – arjoonn
                              Sep 6 '15 at 5:37


















                            6














                            Print has an optional end argument, it is what printed in the end.
                            The default is newline but you can change it to empty string. eg: print("hello world!", end="")






                            share|improve this answer
























                            • it gives a syntax error

                              – thedp
                              Jul 13 '15 at 14:37






                            • 2





                              This is in python 3. In python 2 you can simply print 'something',. The comma at the end prevents the addition of a newline.

                              – arjoonn
                              Sep 6 '15 at 5:37
















                            6












                            6








                            6







                            Print has an optional end argument, it is what printed in the end.
                            The default is newline but you can change it to empty string. eg: print("hello world!", end="")






                            share|improve this answer













                            Print has an optional end argument, it is what printed in the end.
                            The default is newline but you can change it to empty string. eg: print("hello world!", end="")







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 27 '15 at 16:44









                            TulkinRBTulkinRB

                            41258




                            41258













                            • it gives a syntax error

                              – thedp
                              Jul 13 '15 at 14:37






                            • 2





                              This is in python 3. In python 2 you can simply print 'something',. The comma at the end prevents the addition of a newline.

                              – arjoonn
                              Sep 6 '15 at 5:37





















                            • it gives a syntax error

                              – thedp
                              Jul 13 '15 at 14:37






                            • 2





                              This is in python 3. In python 2 you can simply print 'something',. The comma at the end prevents the addition of a newline.

                              – arjoonn
                              Sep 6 '15 at 5:37



















                            it gives a syntax error

                            – thedp
                            Jul 13 '15 at 14:37





                            it gives a syntax error

                            – thedp
                            Jul 13 '15 at 14:37




                            2




                            2





                            This is in python 3. In python 2 you can simply print 'something',. The comma at the end prevents the addition of a newline.

                            – arjoonn
                            Sep 6 '15 at 5:37







                            This is in python 3. In python 2 you can simply print 'something',. The comma at the end prevents the addition of a newline.

                            – arjoonn
                            Sep 6 '15 at 5:37













                            6














                            Most simple:



                            Python 3



                                print(‘r’+’something to be override’,end=‘’)


                            It means it will back the cursor to beginning, than will print something and will end in the same line. If in a loop it will start printing in the same place it starts.






                            share|improve this answer


























                            • Cool! Works in Windows as well.

                              – Shital Shah
                              Feb 13 at 11:59











                            • Thanks this works fine for me! I think that print(’something to be override’, end=‘r’) is simpler, though.

                              – Francesco Boccardo
                              Apr 10 at 9:00
















                            6














                            Most simple:



                            Python 3



                                print(‘r’+’something to be override’,end=‘’)


                            It means it will back the cursor to beginning, than will print something and will end in the same line. If in a loop it will start printing in the same place it starts.






                            share|improve this answer


























                            • Cool! Works in Windows as well.

                              – Shital Shah
                              Feb 13 at 11:59











                            • Thanks this works fine for me! I think that print(’something to be override’, end=‘r’) is simpler, though.

                              – Francesco Boccardo
                              Apr 10 at 9:00














                            6












                            6








                            6







                            Most simple:



                            Python 3



                                print(‘r’+’something to be override’,end=‘’)


                            It means it will back the cursor to beginning, than will print something and will end in the same line. If in a loop it will start printing in the same place it starts.






                            share|improve this answer















                            Most simple:



                            Python 3



                                print(‘r’+’something to be override’,end=‘’)


                            It means it will back the cursor to beginning, than will print something and will end in the same line. If in a loop it will start printing in the same place it starts.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 3 at 15:53

























                            answered Nov 6 '18 at 19:15









                            OctaviusOctavius

                            9327




                            9327













                            • Cool! Works in Windows as well.

                              – Shital Shah
                              Feb 13 at 11:59











                            • Thanks this works fine for me! I think that print(’something to be override’, end=‘r’) is simpler, though.

                              – Francesco Boccardo
                              Apr 10 at 9:00



















                            • Cool! Works in Windows as well.

                              – Shital Shah
                              Feb 13 at 11:59











                            • Thanks this works fine for me! I think that print(’something to be override’, end=‘r’) is simpler, though.

                              – Francesco Boccardo
                              Apr 10 at 9:00

















                            Cool! Works in Windows as well.

                            – Shital Shah
                            Feb 13 at 11:59





                            Cool! Works in Windows as well.

                            – Shital Shah
                            Feb 13 at 11:59













                            Thanks this works fine for me! I think that print(’something to be override’, end=‘r’) is simpler, though.

                            – Francesco Boccardo
                            Apr 10 at 9:00





                            Thanks this works fine for me! I think that print(’something to be override’, end=‘r’) is simpler, though.

                            – Francesco Boccardo
                            Apr 10 at 9:00











                            4














                            If you want to overwrite the previous line (rather than continually adding to it), you can combine r with print(), at the end of the print statement. For example,



                            from time import sleep

                            for i in xrange(0, 10):
                            print("r{0}".format(i)),
                            sleep(.5)

                            print("...DONE!")


                            will count 0 to 9, replacing the old number in the console. The "...DONE!" will print on the same line as the last counter, 9.



                            In your case for the OP, this would allow the console to display percent complete of the install as a "progress bar", where you can define a begin and end character position, and update the markers in between.



                            print("Installing |XXXXXX              | 30%"),





                            share|improve this answer






























                              4














                              If you want to overwrite the previous line (rather than continually adding to it), you can combine r with print(), at the end of the print statement. For example,



                              from time import sleep

                              for i in xrange(0, 10):
                              print("r{0}".format(i)),
                              sleep(.5)

                              print("...DONE!")


                              will count 0 to 9, replacing the old number in the console. The "...DONE!" will print on the same line as the last counter, 9.



                              In your case for the OP, this would allow the console to display percent complete of the install as a "progress bar", where you can define a begin and end character position, and update the markers in between.



                              print("Installing |XXXXXX              | 30%"),





                              share|improve this answer




























                                4












                                4








                                4







                                If you want to overwrite the previous line (rather than continually adding to it), you can combine r with print(), at the end of the print statement. For example,



                                from time import sleep

                                for i in xrange(0, 10):
                                print("r{0}".format(i)),
                                sleep(.5)

                                print("...DONE!")


                                will count 0 to 9, replacing the old number in the console. The "...DONE!" will print on the same line as the last counter, 9.



                                In your case for the OP, this would allow the console to display percent complete of the install as a "progress bar", where you can define a begin and end character position, and update the markers in between.



                                print("Installing |XXXXXX              | 30%"),





                                share|improve this answer















                                If you want to overwrite the previous line (rather than continually adding to it), you can combine r with print(), at the end of the print statement. For example,



                                from time import sleep

                                for i in xrange(0, 10):
                                print("r{0}".format(i)),
                                sleep(.5)

                                print("...DONE!")


                                will count 0 to 9, replacing the old number in the console. The "...DONE!" will print on the same line as the last counter, 9.



                                In your case for the OP, this would allow the console to display percent complete of the install as a "progress bar", where you can define a begin and end character position, and update the markers in between.



                                print("Installing |XXXXXX              | 30%"),






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Oct 9 '15 at 17:50

























                                answered Oct 9 '15 at 16:18









                                Brian BunkerBrian Bunker

                                7516




                                7516























                                    3














                                    Here a 2.7-compatible version derived from the 3.0 version by @Vadim-Zin4uk:



                                    Python 2



                                    import time

                                    for i in range(101): # for 0 to 100
                                    s = str(i) + '%' # string for output
                                    print '{0}r'.format(s), # just print and flush

                                    time.sleep(0.2)


                                    For that matter, the 3.0 solution provided looks a little bloated. For example, the backspace method doesn't make use of the integer argument and could probably be done away with altogether.



                                    Python 3



                                    import time

                                    for i in range(101): # for 0 to 100
                                    s = str(i) + '%' # string for output
                                    print('{0}r'.format(s), end='') # just print and flush

                                    time.sleep(0.2) # sleep for 200ms


                                    Both have been tested and work.






                                    share|improve this answer




























                                      3














                                      Here a 2.7-compatible version derived from the 3.0 version by @Vadim-Zin4uk:



                                      Python 2



                                      import time

                                      for i in range(101): # for 0 to 100
                                      s = str(i) + '%' # string for output
                                      print '{0}r'.format(s), # just print and flush

                                      time.sleep(0.2)


                                      For that matter, the 3.0 solution provided looks a little bloated. For example, the backspace method doesn't make use of the integer argument and could probably be done away with altogether.



                                      Python 3



                                      import time

                                      for i in range(101): # for 0 to 100
                                      s = str(i) + '%' # string for output
                                      print('{0}r'.format(s), end='') # just print and flush

                                      time.sleep(0.2) # sleep for 200ms


                                      Both have been tested and work.






                                      share|improve this answer


























                                        3












                                        3








                                        3







                                        Here a 2.7-compatible version derived from the 3.0 version by @Vadim-Zin4uk:



                                        Python 2



                                        import time

                                        for i in range(101): # for 0 to 100
                                        s = str(i) + '%' # string for output
                                        print '{0}r'.format(s), # just print and flush

                                        time.sleep(0.2)


                                        For that matter, the 3.0 solution provided looks a little bloated. For example, the backspace method doesn't make use of the integer argument and could probably be done away with altogether.



                                        Python 3



                                        import time

                                        for i in range(101): # for 0 to 100
                                        s = str(i) + '%' # string for output
                                        print('{0}r'.format(s), end='') # just print and flush

                                        time.sleep(0.2) # sleep for 200ms


                                        Both have been tested and work.






                                        share|improve this answer













                                        Here a 2.7-compatible version derived from the 3.0 version by @Vadim-Zin4uk:



                                        Python 2



                                        import time

                                        for i in range(101): # for 0 to 100
                                        s = str(i) + '%' # string for output
                                        print '{0}r'.format(s), # just print and flush

                                        time.sleep(0.2)


                                        For that matter, the 3.0 solution provided looks a little bloated. For example, the backspace method doesn't make use of the integer argument and could probably be done away with altogether.



                                        Python 3



                                        import time

                                        for i in range(101): # for 0 to 100
                                        s = str(i) + '%' # string for output
                                        print('{0}r'.format(s), end='') # just print and flush

                                        time.sleep(0.2) # sleep for 200ms


                                        Both have been tested and work.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jan 10 '18 at 19:47









                                        DannidDannid

                                        7781014




                                        7781014























                                            2














                                            This is a very old thread, but here's a very thorough answer and sample code.



                                            r is the string representation of Carriage Return from the ASCII character set. It's the same as octal 015 [chr(0o15)] or hexidecimal 0d [chr(0x0d)] or decimal 13 [chr(13)]. See man ascii for a boring read. It (r) is a pretty portable representation and is easy enough for people to read. It very simply means to move the carriage on the typewriter all the way back to the start without advancing the paper. It's the CR part of CRLF which means Carriage Return and Line Feed.



                                            print() is a function in Python 3. In Python 2 (any version that you'd be interested in using), print can be forced into a function by importing its definition from the __future__ module. The benefit of the print function is that you can specify what to print at the end, overriding the default behavior of n to print a newline at the end of every print() call.



                                            sys.stdout.flush tells Python to flush the output of standard output, which is where you send output with print() unless you specify otherwise. You can also get the same behavior by running with python -u or setting environment variable PYTHONUNBUFFERED=1, thereby skipping the import sys and sys.stdout.flush() calls. The amount you gain by doing that is almost exactly zero and isn't very easy to debug if you conveniently forget that you have to do that step before your application behaves properly.



                                            And a sample. Note that this runs perfectly in Python 2 or 3.



                                            from __future__ import print_function

                                            import sys
                                            import time

                                            ANS = 42
                                            FACTORS = {n for n in range(1, ANS + 1) if ANS % n == 0}

                                            for i in range(1, ANS + 1):
                                            if i in FACTORS:
                                            print('r{0:d}'.format(i), end='')
                                            sys.stdout.flush()
                                            time.sleep(ANS / 100.0)
                                            else:
                                            print()





                                            share|improve this answer
























                                            • I think your answer is more suitable for: stackoverflow.com/questions/45263205/…

                                              – Marc Cayuela Rafols
                                              Oct 25 '17 at 19:06
















                                            2














                                            This is a very old thread, but here's a very thorough answer and sample code.



                                            r is the string representation of Carriage Return from the ASCII character set. It's the same as octal 015 [chr(0o15)] or hexidecimal 0d [chr(0x0d)] or decimal 13 [chr(13)]. See man ascii for a boring read. It (r) is a pretty portable representation and is easy enough for people to read. It very simply means to move the carriage on the typewriter all the way back to the start without advancing the paper. It's the CR part of CRLF which means Carriage Return and Line Feed.



                                            print() is a function in Python 3. In Python 2 (any version that you'd be interested in using), print can be forced into a function by importing its definition from the __future__ module. The benefit of the print function is that you can specify what to print at the end, overriding the default behavior of n to print a newline at the end of every print() call.



                                            sys.stdout.flush tells Python to flush the output of standard output, which is where you send output with print() unless you specify otherwise. You can also get the same behavior by running with python -u or setting environment variable PYTHONUNBUFFERED=1, thereby skipping the import sys and sys.stdout.flush() calls. The amount you gain by doing that is almost exactly zero and isn't very easy to debug if you conveniently forget that you have to do that step before your application behaves properly.



                                            And a sample. Note that this runs perfectly in Python 2 or 3.



                                            from __future__ import print_function

                                            import sys
                                            import time

                                            ANS = 42
                                            FACTORS = {n for n in range(1, ANS + 1) if ANS % n == 0}

                                            for i in range(1, ANS + 1):
                                            if i in FACTORS:
                                            print('r{0:d}'.format(i), end='')
                                            sys.stdout.flush()
                                            time.sleep(ANS / 100.0)
                                            else:
                                            print()





                                            share|improve this answer
























                                            • I think your answer is more suitable for: stackoverflow.com/questions/45263205/…

                                              – Marc Cayuela Rafols
                                              Oct 25 '17 at 19:06














                                            2












                                            2








                                            2







                                            This is a very old thread, but here's a very thorough answer and sample code.



                                            r is the string representation of Carriage Return from the ASCII character set. It's the same as octal 015 [chr(0o15)] or hexidecimal 0d [chr(0x0d)] or decimal 13 [chr(13)]. See man ascii for a boring read. It (r) is a pretty portable representation and is easy enough for people to read. It very simply means to move the carriage on the typewriter all the way back to the start without advancing the paper. It's the CR part of CRLF which means Carriage Return and Line Feed.



                                            print() is a function in Python 3. In Python 2 (any version that you'd be interested in using), print can be forced into a function by importing its definition from the __future__ module. The benefit of the print function is that you can specify what to print at the end, overriding the default behavior of n to print a newline at the end of every print() call.



                                            sys.stdout.flush tells Python to flush the output of standard output, which is where you send output with print() unless you specify otherwise. You can also get the same behavior by running with python -u or setting environment variable PYTHONUNBUFFERED=1, thereby skipping the import sys and sys.stdout.flush() calls. The amount you gain by doing that is almost exactly zero and isn't very easy to debug if you conveniently forget that you have to do that step before your application behaves properly.



                                            And a sample. Note that this runs perfectly in Python 2 or 3.



                                            from __future__ import print_function

                                            import sys
                                            import time

                                            ANS = 42
                                            FACTORS = {n for n in range(1, ANS + 1) if ANS % n == 0}

                                            for i in range(1, ANS + 1):
                                            if i in FACTORS:
                                            print('r{0:d}'.format(i), end='')
                                            sys.stdout.flush()
                                            time.sleep(ANS / 100.0)
                                            else:
                                            print()





                                            share|improve this answer













                                            This is a very old thread, but here's a very thorough answer and sample code.



                                            r is the string representation of Carriage Return from the ASCII character set. It's the same as octal 015 [chr(0o15)] or hexidecimal 0d [chr(0x0d)] or decimal 13 [chr(13)]. See man ascii for a boring read. It (r) is a pretty portable representation and is easy enough for people to read. It very simply means to move the carriage on the typewriter all the way back to the start without advancing the paper. It's the CR part of CRLF which means Carriage Return and Line Feed.



                                            print() is a function in Python 3. In Python 2 (any version that you'd be interested in using), print can be forced into a function by importing its definition from the __future__ module. The benefit of the print function is that you can specify what to print at the end, overriding the default behavior of n to print a newline at the end of every print() call.



                                            sys.stdout.flush tells Python to flush the output of standard output, which is where you send output with print() unless you specify otherwise. You can also get the same behavior by running with python -u or setting environment variable PYTHONUNBUFFERED=1, thereby skipping the import sys and sys.stdout.flush() calls. The amount you gain by doing that is almost exactly zero and isn't very easy to debug if you conveniently forget that you have to do that step before your application behaves properly.



                                            And a sample. Note that this runs perfectly in Python 2 or 3.



                                            from __future__ import print_function

                                            import sys
                                            import time

                                            ANS = 42
                                            FACTORS = {n for n in range(1, ANS + 1) if ANS % n == 0}

                                            for i in range(1, ANS + 1):
                                            if i in FACTORS:
                                            print('r{0:d}'.format(i), end='')
                                            sys.stdout.flush()
                                            time.sleep(ANS / 100.0)
                                            else:
                                            print()






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Oct 25 '17 at 18:20









                                            Chris CosbyChris Cosby

                                            234




                                            234













                                            • I think your answer is more suitable for: stackoverflow.com/questions/45263205/…

                                              – Marc Cayuela Rafols
                                              Oct 25 '17 at 19:06



















                                            • I think your answer is more suitable for: stackoverflow.com/questions/45263205/…

                                              – Marc Cayuela Rafols
                                              Oct 25 '17 at 19:06

















                                            I think your answer is more suitable for: stackoverflow.com/questions/45263205/…

                                            – Marc Cayuela Rafols
                                            Oct 25 '17 at 19:06





                                            I think your answer is more suitable for: stackoverflow.com/questions/45263205/…

                                            – Marc Cayuela Rafols
                                            Oct 25 '17 at 19:06











                                            1














                                            Just in case you have pre-stored the values in an array, you can call them in the following format:



                                            for i in range(0,n):
                                            print arr[i],





                                            share|improve this answer




























                                              1














                                              Just in case you have pre-stored the values in an array, you can call them in the following format:



                                              for i in range(0,n):
                                              print arr[i],





                                              share|improve this answer


























                                                1












                                                1








                                                1







                                                Just in case you have pre-stored the values in an array, you can call them in the following format:



                                                for i in range(0,n):
                                                print arr[i],





                                                share|improve this answer













                                                Just in case you have pre-stored the values in an array, you can call them in the following format:



                                                for i in range(0,n):
                                                print arr[i],






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Oct 16 '16 at 23:20









                                                k20392k20392

                                                111




                                                111























                                                    -3














                                                    I found this solution, and it's working on Python 2.7



                                                    # Working on Python 2.7 Linux

                                                    import time
                                                    import sys


                                                    def backspace(n):
                                                    print('r', end='') # use 'r' to go back


                                                    for i in range(101): # for 0 to 100
                                                    s = str(i) + '%' # string for output
                                                    sys.stdout.write(string)
                                                    backspace(len(s)) # back for n chars
                                                    sys.stdout.flush()
                                                    time.sleep(0.2) # sleep for 200ms





                                                    share|improve this answer





















                                                    • 1





                                                      That print is incorrect syntax for Python 2.7 and the code doesn't work even with correct syntax.

                                                      – Cory Madden
                                                      Aug 6 '17 at 20:47
















                                                    -3














                                                    I found this solution, and it's working on Python 2.7



                                                    # Working on Python 2.7 Linux

                                                    import time
                                                    import sys


                                                    def backspace(n):
                                                    print('r', end='') # use 'r' to go back


                                                    for i in range(101): # for 0 to 100
                                                    s = str(i) + '%' # string for output
                                                    sys.stdout.write(string)
                                                    backspace(len(s)) # back for n chars
                                                    sys.stdout.flush()
                                                    time.sleep(0.2) # sleep for 200ms





                                                    share|improve this answer





















                                                    • 1





                                                      That print is incorrect syntax for Python 2.7 and the code doesn't work even with correct syntax.

                                                      – Cory Madden
                                                      Aug 6 '17 at 20:47














                                                    -3












                                                    -3








                                                    -3







                                                    I found this solution, and it's working on Python 2.7



                                                    # Working on Python 2.7 Linux

                                                    import time
                                                    import sys


                                                    def backspace(n):
                                                    print('r', end='') # use 'r' to go back


                                                    for i in range(101): # for 0 to 100
                                                    s = str(i) + '%' # string for output
                                                    sys.stdout.write(string)
                                                    backspace(len(s)) # back for n chars
                                                    sys.stdout.flush()
                                                    time.sleep(0.2) # sleep for 200ms





                                                    share|improve this answer















                                                    I found this solution, and it's working on Python 2.7



                                                    # Working on Python 2.7 Linux

                                                    import time
                                                    import sys


                                                    def backspace(n):
                                                    print('r', end='') # use 'r' to go back


                                                    for i in range(101): # for 0 to 100
                                                    s = str(i) + '%' # string for output
                                                    sys.stdout.write(string)
                                                    backspace(len(s)) # back for n chars
                                                    sys.stdout.flush()
                                                    time.sleep(0.2) # sleep for 200ms






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Jul 23 '17 at 8:34









                                                    ideasman42

                                                    13.3k673140




                                                    13.3k673140










                                                    answered Dec 7 '16 at 12:51









                                                    Luis SilvaLuis Silva

                                                    23




                                                    23








                                                    • 1





                                                      That print is incorrect syntax for Python 2.7 and the code doesn't work even with correct syntax.

                                                      – Cory Madden
                                                      Aug 6 '17 at 20:47














                                                    • 1





                                                      That print is incorrect syntax for Python 2.7 and the code doesn't work even with correct syntax.

                                                      – Cory Madden
                                                      Aug 6 '17 at 20:47








                                                    1




                                                    1





                                                    That print is incorrect syntax for Python 2.7 and the code doesn't work even with correct syntax.

                                                    – Cory Madden
                                                    Aug 6 '17 at 20:47





                                                    That print is incorrect syntax for Python 2.7 and the code doesn't work even with correct syntax.

                                                    – Cory Madden
                                                    Aug 6 '17 at 20:47


















                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5598181%2fpython-multiple-prints-on-the-same-line%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    MongoDB - Not Authorized To Execute Command

                                                    Npm cannot find a required file even through it is in the searched directory

                                                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith