472,988 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 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 8612
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

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

Similar topics

4
by: Pekka Niiranen | last post by:
Hi, I have a multiuser script, that I would like to convert to Python. The users open simultaneous telnet -sessions from win2000 to an unix machine and possibly edit unicode textfiles. Currently...
5
by: Darren Dale | last post by:
I am using Emacs Python mode, and my project involves reading large datafiles and processing large arrays. I have some code that reports the progress during these time consuming processes. It works...
9
by: Paul Watson | last post by:
I thought that using a comma at the end of a print statement would suppress printing of a newline. Am I misunderstanding this feature? How can I use print and not have a newline appended at the...
1
by: YERVANT | last post by:
I would like use the Print Screen Button to copy a part of page using ASP. En fact, I use a data access page of a chart (pivot) created by access. And I would like copy only the chart. I think...
12
by: lemon97 | last post by:
Hi, Is there a function that split a string into groups, containing an "x" amount of characters? Ex. TheFunction("Hello World",3) Returns:
1
by: yichun.wei | last post by:
Perl has the ability to do the following: print <<EOF; ...reams of text goes here... EOF Is there a Python equivalent of the above Perl code? This thread has previous discussion on the...
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
2
by: Phoe6 | last post by:
print and softspace in python In python, whenever you use >>>print statement it will append a newline by default. If you don't want newline to be appended, you got use a comma at the end (>>>print...
13
by: damonwischik | last post by:
I'd like to print out a unicode string. I'm running Python inside Emacs, which understands utf-8, so I want to force Python to send utf-8 to sys.stdout. From what I've googled, I think I need...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.