Concatenation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pyn00b
    New Member
    • Feb 2008
    • 6

    Concatenation

    After getting raw_input from the user, I want to print a sentence that ends in an integer and then the "." character. Of course, if I print using a comma (,) I don't get an error but there is an ugly space between the integer and the period. But if I try "+" then I obviously get a str/int concatenation error.

    One way to solve this is to convert the int into a str and then "+" works. But is there an easier way without having to call the str() method?
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by pyn00b
    After getting raw_input from the user, I want to print a sentence that ends in an integer and then the "." character. Of course, if I print using a comma (,) I don't get an error but there is an ugly space between the integer and the period. But if I try "+" then I obviously get a str/int concatenation error.

    One way to solve this is to convert the int into a str and then "+" works. But is there an easier way without having to call the str() method?
    Um, what language are you working in?

    Comment

    • pyn00b
      New Member
      • Feb 2008
      • 6

      #3
      Originally posted by numberwhun
      Um, what language are you working in?
      Python. So here is some code...

      num = raw_input("Pick a number: ")
      num = int(num)
      print "Your number times 5 is", num * 5, "."

      This produces a space between num and the period. If I try to use "+" I get an error trying to concatenate a str and int. It's possible to print str(num) and then concatenate that result with the period to eliminate the space. But I was wondering if there was a more obvious way than calling the str() function.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Moved the thread to the Python forum.

        Comment

        • pyn00b
          New Member
          • Feb 2008
          • 6

          #5
          Originally posted by Ganon11
          Moved the thread to the Python forum.
          Thanks! Sorry about that. New here. :)

          Comment

          • dshimer
            Recognized Expert New Member
            • Dec 2006
            • 136

            #6
            And don't forget Code tags.

            My favorite way (because there are several) is using string formatting.
            Code:
            >>> num = raw_input("Pick a number: ")
            >>> num = int(num)
            >>> print "Your number times 5 is %d."%(num * 5)
            Your number times 5 is 30.
            You could also move the int() call to tighten the code a little.
            Code:
            >>> num = int(raw_input("Pick a number: "))
            >>> print "Your number times 5 is %d."%(num * 5)
            Your number times 5 is 40.
            or for that matter
            Code:
            print "Your number times 5 is %d."%(int(raw_input("Pick a number: ")) * 5)
            Once you start worrying about what happens if the user enters a non numeric value like "a" you will want to explore the try: and except: block.

            Comment

            • jlm699
              Contributor
              • Jul 2007
              • 314

              #7
              Originally posted by pyn00b
              [CODE=python]
              num = raw_input("Pick a number: ")
              num = int(num)
              print "Your number times 5 is", num * 5, "."
              [/CODE]
              This produces a space between num and the period. If I try to use "+" I get an error trying to concatenate a str and int. It's possible to print str(num) and then concatenate that result with the period to eliminate the space. But I was wondering if there was a more obvious way than calling the str() function.
              So why not use the str() conversion? It's probably just as easy...

              [CODE=python]
              num = int(raw_input(" Pick a number: "))
              print "Your number times 5 is " + str(num * 5) + "."
              [/CODE]

              I guess it's a matter of preference, but that's how I do all my string/int concatenations. ..

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                I try to avoid string concatenation where possible. String formating with %s uses str() to generate the string.[code=Python]num = int(raw_input(" Pick a number: "))
                print "Your number times 5 is %s." % (num * 5)[/code]

                Comment

                • pyn00b
                  New Member
                  • Feb 2008
                  • 6

                  #9
                  dshimer, jlm699, and bvdet - thanks to you all for responding. That was very helpful and educational!

                  Comment

                  Working...