471,338 Members | 1,250 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

print control characters in python shell

56
I have problem printing in console. How can i print a character that will be printed in one line only.Like a countdown timer that will output the character 10-0 in one line:
Expand|Select|Wrap|Line Numbers
  1. x=10
  2. while x!=10:
  3.      print x
  4.      x-=1
  5.  
output should not be like this..
10
9
8
etc..

but it should print in one line like this..

10 then '10' will become 9 then 8 etc...

hope you understand what i am trying to ask....
Oct 11 '07 #1
9 8343
bartonc
6,596 Expert 4TB
I have problem printing in console. How can i print a character that will be printed in one line only.Like a countdown timer that will output the character 10-0 in one line:
Expand|Select|Wrap|Line Numbers
  1. x=10
  2. while x!=10:
  3.      print x
  4.      x-=1
  5.  
output should not be like this..
10
9
8
etc..

but it should print in one line like this..

10 then '10' will become 9 then 8 etc...

hope you understand what i am trying to ask....
print accepts a comma to supress the newline at the end and add a space.
Expand|Select|Wrap|Line Numbers
  1. x = 10
  2. while x > -1:
  3.      print x,
  4.      x -= 1
Speaking of spaces: They increase readability of your code greatly.
Oct 11 '07 #2
elcron
43
Or if you prefer to have more control over the spaces in between you could use the following:
Expand|Select|Wrap|Line Numbers
  1. x = 10
  2. spacer = ' - ' # replace with whatever you want as long as it's a string
  3. toPrint = ""
  4.     while x > -1:
  5.         toPrint += str(x)
  6.         x -= 1
  7.         if  x > -1: # prevents trailing spacer
  8.             toPrint += spacer
  9. print toPrint
  10. # 10 - 9 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1 - 0
  11.  
Oct 11 '07 #3
rhitam30111985
112 100+
I have problem printing in console. How can i print a character that will be printed in one line only.Like a countdown timer that will output the character 10-0 in one line:
Expand|Select|Wrap|Line Numbers
  1. x=10
  2. while x!=10:
  3.      print x
  4.      x-=1
  5.  
output should not be like this..
10
9
8
etc..

but it should print in one line like this..

10 then '10' will become 9 then 8 etc...

hope you understand what i am trying to ask....
I am not sure if the following is what you are trying to do:

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import time
  3. x=10
  4. while x>-1:
  5.      print x
  6.      time.sleep(1)
  7.      os.system('clear')
  8.      x-=1
  9.  
Oct 12 '07 #4
heiro
56
I think rhitam30111985 get the idea but the problem is, what if i printed many string on the screen before the timer starts. So if we will use the cls command all of these will be deleted on the screen.

Lets imagine another thing,let say we are making a progress bar that will be displaying 0% to 100%.How will I do that??Once again thanks guys...
Oct 12 '07 #5
I think rhitam30111985 get the idea but the problem is, what if i printed many string on the screen before the timer starts. So if we will use the cls command all of these will be deleted on the screen.

Lets imagine another thing,let say we are making a progress bar that will be displaying 0% to 100%.How will I do that??Once again thanks guys...
You mean like this?
Expand|Select|Wrap|Line Numbers
  1. x = 10
  2. while x > -1:
  3.     print "\b%i" % x,
  4.     for i in range(len(str(x))):
  5.         print "\b\b",
  6.     x -= 1
You may have to change where and when "\b" is printed, but that should be about right.
Oct 12 '07 #6
Here's a script that I wrote recently trying to accomplish something similar.

Expand|Select|Wrap|Line Numbers
  1. import time
  2.  
  3. spinner = "-\|/"
  4. starttime = time.time()
  5. duration = 5
  6.  
  7. print " ",
  8.  
  9. while time.time() - starttime < duration:
  10.     for item in spinner:
  11.         print "\b\b" + item,
  12.         time.sleep(0.05)
Oct 12 '07 #7
heiro
56
Here's a script that I wrote recently trying to accomplish something similar.

Expand|Select|Wrap|Line Numbers
  1. import time
  2.  
  3. spinner = "-\|/"
  4. starttime = time.time()
  5. duration = 5
  6.  
  7. print " ",
  8.  
  9. while time.time() - starttime < duration:
  10.     for item in spinner:
  11.         print "\b\b" + item,
  12.         time.sleep(0.05)

yah this is right!!!!!
thanks for this KaezarRex.Would you mind if i ask what "\b" is for?
Oct 12 '07 #8
yah this is right!!!!!
thanks for this KaezarRex.Would you mind if i ask what "\b" is for?
It's an escape sequence, so pretty much a string that represents a symbol you can't type. In this case "\b" means "backspace", so you are backing up the cursor over the number you just printed. You have to use two in a row because a comma in a print statement prints an extra " ".
Oct 12 '07 #9
heiro
56
It's an escape sequence, so pretty much a string that represents a symbol you can't type. In this case "\b" means "backspace", so you are backing up the cursor over the number you just printed. You have to use two in a row because a comma in a print statement prints an extra " ".

thank you so much for this......i learned something new from u....
TYVM
Oct 12 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Pekka Niiranen | last post: by
5 posts views Thread by Darren Dale | last post: by
9 posts views Thread by Paul Watson | last post: by
69 posts views Thread by Edward K Ream | last post: by
2 posts views Thread by Phoe6 | last post: by
13 posts views Thread by damonwischik | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.