Hack Forums
[Python] Changing a part of a string, but still displaying the other part. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Changing a part of a string, but still displaying the other part. (/showthread.php?tid=3746466)



Changing a part of a string, but still displaying the other part. - ๖ۣۜAlexander - Sep 10, 2013

So, I've got a bit stuck and I've found no answer.

What I actually want is something like this:

Code
Time left: 4
Time left: 3

But I don't want the 'Time Left' to be printed everytime but just for the number (4) to change.

How can I do this with python? Or should I clear the screen everytime and reprint the string?

Cheers!


* - ßlack мesa - Sep 10, 2013

So, you want the console to print 'Time Left: n' on a single line and for the line to 'update' to the console with a new value for n?
Try this,
Code
import time, sys

for i in range(100):
    time.sleep(1)
    sys.stdout.write("\rTime Left:%d%%" %i)
    sys.stdout.flush()



* - PigBacon - Sep 10, 2013

If you're sneaky you can just print \d (the delete character) then print the correction.


* - Logicmorg - Sep 11, 2013

Does the entire thing have to be 1 string?
For example would this be acceptable:
PHP Code

<?php 
import time
import sys

x 
= 100; 
for 
i in range(0, 100):
  
time.sleep(1)
  print(
"Time left: " + x);
  
x = x -1; 

EDIT: Just realized this is wrong, x should be a string when being used by the function print. Since OP has said this script is not the one he is looking for please disregard this code.


* - ๖ۣۜAlexander - Sep 11, 2013

(Sep 11, 2013 06:49 AM)Logicmorg Wrote:  Does the entire thing have to be 1 string?
For example would this be acceptable:
PHP Code

<?php 
import time
import sys

x 
= 100; 
for 
i in range(0, 100):
  
time.sleep(1)
  print(
"Time left: " + x);
  
x = x -1; 

That will just print the string over and over after a second.
Like this:

Code
Time left: 4
Time left: 3

I don't want that. I just want the number to change but for the 'time left' to be on the same line.


* - MR.FREE - Sep 11, 2013

use string specifiers with a loop should handle that for you?
wait I see what your asking now...


* - Logicmorg - Sep 11, 2013

(Sep 11, 2013 07:39 AM)The Ð Wrote:  That will just print the string over and over after a second.
Like this:

Code
Time left: 4
Time left: 3

I don't want that. I just want the number to change but for the 'time left' to be on the same line.

Okay I see what you are saying ( I think )
You want to display
Time left: 4
then after second goes by the 4 is then replaced with a 3, is this correct?


* - ๖ۣۜAlexander - Sep 11, 2013

(Sep 11, 2013 08:12 AM)Logicmorg Wrote:  Okay I see what you are saying ( I think )
You want to display
Time left: 4
then after second goes by the 4 is then replaced with a 3, is this correct?

Yea, that's correct! All on the same line :)


* - kieranlav - Sep 11, 2013

There is a really simple method you could use if you wish.

Code
import time
for i in range(100):
    time_left = 100 - i
    time.sleep(1)
    print("You have {0} seconds left.".format(time_left))

This is not really a very good way about doing this but I though I should provide a way using very simple code. Not this is not the best or even a good way of doing this. Also this code is in python 3.x format.

--

Just took a look through the later parts of the thread and I see that this is not really what you want. I will try and think of a way to do what you want gimmie a sec.


* - ßlack мesa - Sep 11, 2013

(Sep 11, 2013 08:22 AM)The Ð Wrote:  Yea, that's correct! All on the same line :)
I did this for you earlier... Check at the top.


* - ๖ۣۜAlexander - Sep 11, 2013

(Sep 11, 2013 09:49 AM)ßlack мesa Wrote:  I did this for you earlier... Check at the top.

I don't have access to pc now, so I'm just taking suggestions which I can use later when I'm on PC :)


* - kieranlav - Sep 11, 2013

(Sep 11, 2013 10:21 AM)The Ð Wrote:  I don't have access to pc now, so I'm just taking suggestions which I can use later when I'm on PC :)

Code
import time
import sys

for i in range(4, 0, -1):
    print('\rTime left:', i, end='')
    sys.stdout.flush()
    time.sleep(1)

There you go, change the range accordingly. I would also like to note that this wont work in the idle I don't think. It should be ran how a user would run it.

Here is the version of the code for 2.x

Code
import time
import sys

for i in range(4, 0, -1):
    print '\rTime left:', i,
    sys.stdout.flush()
    time.sleep(1)