concatenating strings

Discussion in 'Python' started by EHC, Dec 15, 2006.

  1. EHC

    EHC Guest

    hello!

    since i am a py noob, please bear with me ; )

    how is it possible to concat a string and an integer in a
    print-command? i've tried

    print "This robot is named %s. The current speed setting is %d, and %s
    has a lifetime of %d" % (self.name , self.speed , self.name)

    as well as

    print "This robot is named %s. The current speed setting is %d, and %s
    has a lifetime of %d" & self.name % self.speed % self.name

    though nothing works out...

    background is a class named Robot with members speed, name, etc...

    tia,
    Erich
     
    EHC, Dec 15, 2006
    #1
    1. Advertising

  2. EHC a écrit :
    > hello!
    >
    > since i am a py noob, please bear with me ; )
    >
    > how is it possible to concat a string and an integer in a
    > print-command? i've tried
    >
    > print "This robot is named %s. The current speed setting is %d, and %s
    > has a lifetime of %d" % (self.name , self.speed , self.name)


    Four % formating with only three arguments to format. It cannot work...

    print "This robot is named %s. The current speed setting is %d, and %s
    has a lifetime of %d" % (self.name , self.speed , self.name, self.lifetime)

    ....

    > background is a class named Robot with members speed, name, etc...


    May try this too:
    print "This robot is named %(name)s. The current speed setting is
    %(speed)d, and %(name)s has a lifetime of %(lifetime)d" % self.__dict__

    [note: liftefime may be a dynamically calculated value, and should be
    providen via an accessor attribute]
     
    Laurent Pointal, Dec 15, 2006
    #2
    1. Advertising

  3. EHC

    Erich Pul Guest

    thank you, i just plainly overlooked it ; )

    now it works
     
    Erich Pul, Dec 15, 2006
    #3
  4. Caleb Hattingh, Dec 16, 2006
    #4
    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. Stan Horwitz
    Replies:
    2
    Views:
    2,742
    Stan Horwitz
    Feb 15, 2006
  2. Replies:
    11
    Views:
    627
    Karl Heinz Buchegger
    Apr 8, 2005
  3. John Henry

    Concatenating strings

    John Henry, Jul 1, 2006, in forum: Python
    Replies:
    1
    Views:
    307
    Steven Bethard
    Jul 1, 2006
  4. John Henry

    Concatenating strings

    John Henry, Jul 1, 2006, in forum: Python
    Replies:
    1
    Views:
    333
    Robert Kern
    Jul 1, 2006
  5. c

    Help me with Concatenating strings

    c, Sep 24, 2006, in forum: C Programming
    Replies:
    21
    Views:
    618
    Chris Torek
    Oct 15, 2006
Loading...

Share This Page