Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why does this (not) work?

Reply
Thread Tools

Why does this (not) work?

 
 
Michael C. Neel
Guest
Posts: n/a
 
      08-19-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM



Advertisments