concatenating strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • EHC

    #1

    concatenating strings

    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

  • Laurent Pointal

    #2
    Re: concatenating strings

    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]

    Comment

    • Erich Pul

      #3
      Re: concatenating strings

      thank you, i just plainly overlooked it ; )

      now it works

      Comment

      • Caleb Hattingh

        #4
        Re: concatenating strings

        Hi Erich

        If you're going to be doing a lot of string substitution, you should
        look at the Templating support in the library:



        and (a little bit fancier):



        Regards
        Caleb


        On Dec 15, 12:18 pm, "Erich Pul" <erich....@blac kbox.netwrote:
        thank you, i just plainly overlooked it ; )
        >
        now it works

        Comment

        Working...