![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 65
Rep Power: 3
![]() |
String formatting with tuples
I'm having trouble printing out tuples.
Say, I have a function "joker": def joker(*args): print "%s" %args joker("dad", "dasdsad", "dasdas")ERROR: not all arguments converted during string formatting print args 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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
String formatting in Python either uses a single variable:
"Hello %s" % "World" => "Hello World" "Name: %s, age: %d" % ("Frank", 27) => "Name: Frank, age: 27"The print command uses the repr function to show a human-readable representation of the data structure. So this: print (1, 2) print "%s" % repr((1, 2)) tup = ("Hello", "World")
print ":".join(tup) => Hello:Worldtup = ("Hello", 1)
print ", ".join(str(x) for x in tup) => Hello, 1tup = ("Hello", 1)
print ", ".join(map(str, tup)) => Hello, 1tup = ("Hello", 1)
print ", ".join(map(repr, tup)) => "Hello", 1 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |