string formatter for tuple

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

    #1

    string formatter for tuple

    I have the following in my code

    a = (1,2,3)
    print "a = %s" %a

    But when I run this, I get:

    TypeError: not all arguments converted during string formatting

    Now I realize why this happens, a is actually 3 elements when the print
    statement is only expecting to print one value. I tried

    print "a = %s" %(a)

    but I got the same error.

    How can I print a tuple with a single string format?
    Thanks,
    Jeremy

  • Tim Chase

    #2
    Re: string formatter for tuple

    a = (1,2,3)
    print "a = %s" %a
    >
    But when I run this, I get:
    >
    TypeError: not all arguments converted during string formatting
    >
    Now I realize why this happens, a is actually 3 elements when the print
    statement is only expecting to print one value. I tried
    >
    print "a = %s" %(a)
    >
    but I got the same error.
    >
    How can I print a tuple with a single string format?

    You can try

    print "a = %s" % str(a)

    or

    print "a = %s" % repr(a)

    which should both do the trick.

    -tkc



    Comment

    • Steve Holden

      #3
      Re: string formatter for tuple

      jeremito wrote:
      I have the following in my code
      >
      a = (1,2,3)
      print "a = %s" %a
      >
      But when I run this, I get:
      >
      TypeError: not all arguments converted during string formatting
      >
      Now I realize why this happens, a is actually 3 elements when the print
      statement is only expecting to print one value. I tried
      >
      print "a = %s" %(a)
      >
      but I got the same error.
      >
      How can I print a tuple with a single string format?
      Thanks,
      Jeremy
      >
      Try

      print "a = %s" % (a, )

      The exception to the "a single object can be formatted as a single
      argument rather than a tuple" rule is when that single object is itself
      a tuple!

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://holdenweb.blogspot.com
      Recent Ramblings http://del.icio.us/steve.holden

      Comment

      • Wojciech Mu³a

        #4
        Re: string formatter for tuple

        Tim Chase wrote:
        print "a = %s" % repr(a)
        or
        print "a = %r" % (a,)

        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: string formatter for tuple

          In <1162481557.791 817.41690@b28g2 000cwb.googlegr oups.com>, jeremito wrote:
          I have the following in my code
          >
          a = (1,2,3)
          print "a = %s" %a
          >
          But when I run this, I get:
          >
          TypeError: not all arguments converted during string formatting
          >
          Now I realize why this happens, a is actually 3 elements when the print
          statement is only expecting to print one value. I tried
          >
          print "a = %s" %(a)
          >
          but I got the same error.
          >
          How can I print a tuple with a single string format?
          Put the tuple in a tuple:

          print 'a = %s" % (a,)

          And remember: not the parenthesis create a tuple but the comma!

          Ciao,
          Marc 'BlackJack' Rintsch

          Comment

          • skip@pobox.com

            #6
            Re: string formatter for tuple

            >>>>"Tim" == Tim Chase <python.list@ti m.thechases.com writes:
            >How can I print a tuple with a single string format?
            Tim print "a = %s" % str(a)
            Timor
            Tim print "a = %s" % repr(a)

            Or wrap the tuple in a tuple:

            print "a = %s" % (a,)

            Skip

            Comment

            • jeremito

              #7
              Re: string formatter for tuple


              skip@pobox.com wrote:
              >>>"Tim" == Tim Chase <python.list@ti m.thechases.com writes:
              >
              >How can I print a tuple with a single string format?
              >
              Tim print "a = %s" % str(a)
              Timor
              Tim print "a = %s" % repr(a)
              >
              Or wrap the tuple in a tuple:
              >
              print "a = %s" % (a,)
              >
              Skip
              Thank you all very much for your help.
              Jeremy

              Comment

              Working...