Why does this (not) work?

Discussion in 'Python' started by Michael C. Neel, Aug 19, 2003.

  1. I've got this string in which I need to sub in the same word several
    times; i.e:

    >>> "%s - %s - %s" % ("test","test","test")

    'test - test - test'
    >>>


    But I want to use the * to make life easier to read, so I tried:

    >>> ("test",)*3

    ('test', 'test', 'test')
    >>> "%s - %s - %s" % ("test",)*3

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: not enough arguments for format string
    >>>


    Which seemed like a good idea, but after some playing around I found
    that:

    >>> eval(str(("test",)*3))

    ('test', 'test', 'test')
    >>> "%s - %s - %s" % eval(str(("test",)*3))

    'test - test - test'
    >>>


    Did work. Odd because:

    >>> type(("test",)*3)

    <type 'tuple'>
    >>> type(eval(str(("test",)*3)))

    <type 'tuple'>
    >>>


    Any idea why the tuple to str to tuple works and not the tuple straight?

    Mike
    Michael C. Neel, Aug 19, 2003
    #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. jblazi
    Replies:
    5
    Views:
    419
    jblazi
    Aug 16, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    443
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    853
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,737
    Smokey Grindel
    Dec 2, 2006
  5. Tarun
    Replies:
    5
    Views:
    380
    Tarun
    Jul 14, 2005
Loading...

Share This Page