Thread: string altering

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    58
    Rep Power
    11

    string altering


    i want to make a clock thing tha is just

    12:00

    and that is it. Is there a way to change this on the same line without printing the variable again. Just like a normal clock on a computer.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    27
    Rep Power
    0
    Here's my stab at the question, hope it helps.

    Code:
    from datetime import datetime
    
    current_time = datetime.today()
    display_str = "%2d:%2d"%( current_time.hour, current_time.minute )
    The 2 in %2d ensures that you always have (at least) two character spaces. For example:

    Code:
    >>> "%2d"%4
    ' 4'
    >>> "%2d"%44
    '44'
    >>> "%2d"%444
    '444'
    You probably want to modify this, however, to be %02d. This will fill the empty spaces with 0's, such that:

    Code:
    >>> "%02d"%4
    '04'
    >>> "%02d"%44
    '44'
    >>> "%02d"%444
    '444'
    This will get you the desired string format of a system clock. But as for a way to change this on the same line without printing the variable again, could you please elaborate?

    Derrick
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can use the \backspace character to remove the characters from the console, so then all you have to do is print the new string.

    Code:
    #!/usr/bin/env python
    
    times = ('01:00', '02:00', '03:00', '04:00', '05:00', '06:00')
    
    for time in times:
        print time + '\b\b\b\b\b\b',
    Note: I havnt tested this but you should get the general idea.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    58
    Rep Power
    11
    its basically a normal clock like the one on youre computer screen. every minute it would change but there is still only 4 numbers total

IMN logo majestic logo threadwatch logo seochat tools logo