Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2007, 11:20 AM   #1
kurt
Programmer
 
Join Date: Oct 2005
Posts: 65
Rep Power: 3 kurt is on a distinguished road
String formatting with tuples

I'm having trouble printing out tuples.

Say, I have a function "joker":

def joker(*args):
print "%s" %args
and I call the function with:

joker("dad", "dasdsad", "dasdas")
I get:

ERROR: not all arguments converted during string formatting
Well, putting
print args
would print the whole tuple, but what i'm trying to acheive is printing the tuple without the parenthesis. I'm ok with the comma.

Can get this to work. Can you guys help?

Thanks in advance.

p/s: I'm also wondering what does the red part do?
command="dasdasd"
args = ("dasdas","dasdas")
print(command%args)

Last edited by kurt; Feb 28th, 2007 at 11:55 AM.
kurt is offline   Reply With Quote
Old Feb 28th, 2007, 12:53 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
String formatting in Python either uses a single variable:
"Hello %s" % "World"   =>   "Hello World"
Or a tuple of variables:
"Name: %s, age: %d" % ("Frank", 27)    =>   "Name: Frank, age: 27"
This is why you can't print out the tuple directly.

The print command uses the repr function to show a human-readable representation of the data structure. So this:
print (1, 2)
Is equivalent to:
print "%s" % repr((1, 2))
In order to get some other representation of the tuple, such as without the brackets, you need to convert it into a string. The easiest way to do this is via the join method:
tup = ("Hello", "World")
print ":".join(tup)    =>    Hello:World
The only problem with this is that if your tuple contains non-strings, they won't join together. So you first have to convert them, either by a generator comprehension:
tup = ("Hello", 1)
print ", ".join(str(x) for x in tup)    =>    Hello, 1
Or via map, which applies a function to each element:
tup = ("Hello", 1)
print ", ".join(map(str, tup))   =>    Hello, 1
If you want the representation of the object, use repr instead of str:
tup = ("Hello", 1)
print ", ".join(map(repr, tup))    =>    "Hello", 1
Arevos is offline   Reply With Quote
Old Mar 1st, 2007, 6:03 PM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
How about something like this:
def joker(*args):
    for item in args:
        print "%s" % item,

joker("dad", "dasdsad", "dasdas")
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function Parameters grimpirate PHP 10 Mar 14th, 2007 6:55 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 5:45 PM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 10:38 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 12:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC