Re: help to code...

Discussion in 'Python' started by Mark Lawrence, May 2, 2013.

  1. On 02/05/2013 15:59, Chris Angelico wrote:
    > On Thu, May 2, 2013 at 11:50 PM, leonardo selmi <> wrote:
    >> dear python community,
    >>
    >> i wrote the following program:
    >>
    >> print str(current_month) + '/' + str(current_day) + '/' + str(current_year)
    >> +' '+
    >> print str(current_hour) + str(current_minute) + str(current_second)
    >>
    >> SyntaxError: invalid syntax
    >>
    >> how can i write the last two lines correctly?

    >
    > You're doing two separate print statements. Either join them into one
    > (if you want it to be one line), or drop the last + on the first line,
    > which is causing your syntax error. But there's an even easier way to
    > do this: Use formatted printing.
    >
    > print("%d/%d/%d
    > %d%d%d"%(current_month,current_day,current_year,current_hour,current_minute,current_second))
    >
    > Or, since you're getting those straight from 'now':
    >
    > print("%d/%d/%d
    > %d%d%d"%(now.month,now.day,now.year,now.hour,now.minute,now.second))
    >
    > I strongly suspect that you want to put delimiters in the time, though
    > (colons, perhaps?). It'd be really nice, by the way, if you'd avoid
    > the messy American format date with the month first; put the year
    > first and it's unambiguous!
    >
    > ChrisA
    >


    Better IMHO is to use strftime
    http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
    so the complete code could be

    from datetime import datetime
    print(datetime.now().strftime('%m/%Y/%d %H %m %S'))

    --
    If you're using GoogleCrap™ please read this
    http://wiki.python.org/moin/GoogleGroupsPython.

    Mark Lawrence
    Mark Lawrence, May 2, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,179
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Tee
    Replies:
    1
    Views:
    4,108
    Raterus
    Jun 24, 2004
  3. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: help with repeater, how to code it in code-behind page

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    460
    Ireney Berezniak
    Jun 25, 2004
  4. keithb
    Replies:
    1
    Views:
    888
    Bruce Barker
    Mar 29, 2006
  5. tone
    Replies:
    4
    Views:
    437
    Hywel Jenkins
    Nov 19, 2003
Loading...

Share This Page