Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help me!
Index -> Programming, Python -> Python Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AznTiger




PostPosted: Sun Mar 09, 2008 6:34 pm   Post subject: Help me!

How do you print something, followed by an integer than another string on the same line lets say "you have n butterflys" and n = 10

i.e. in java its System.out.println("you have " + n + "butterflies"); how would you do this in python?
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Sun Mar 09, 2008 7:00 pm   Post subject: RE:Help me!

I have never used python (... yet), but you are looking for the print command I think. Shouldn't this work :

code:
print "a", 1, "b"
AznTiger




PostPosted: Sun Mar 09, 2008 7:05 pm   Post subject: RE:Help me!

Kay thanks. I've tried everything but that lol and yes it does work
wtd




PostPosted: Sun Mar 09, 2008 10:23 pm   Post subject: RE:Help me!

Not as nice as it could be:

Python:
print "You have %d butterflies" % 10
AznTiger




PostPosted: Mon Mar 10, 2008 3:17 pm   Post subject: RE:Help me!

alright thanks
AznTiger




PostPosted: Mon Mar 10, 2008 6:58 pm   Post subject: RE:Help me!

Now how would you do that for floats instead?
richcash




PostPosted: Mon Mar 10, 2008 8:20 pm   Post subject: RE:Help me!

I believe it is %f instead of %d.

Here are some others.
AznTiger




PostPosted: Tue Mar 11, 2008 7:54 pm   Post subject: RE:Help me!

and (last question, I promise) how would you do it for many different strings in one line?
Sponsor
Sponsor
Sponsor
sponsor
syntax_error




PostPosted: Tue Mar 11, 2008 10:00 pm   Post subject: Re: Help me!

never used python but based on the above example and google

here
Python:

print "this line of text",
print "will not stop due to the COMMA"


again try that out not 100% sure

btw no need for that to be the last question
richcash




PostPosted: Wed Mar 12, 2008 12:42 am   Post subject: Re: RE:Help me!

@syntax_error : I don't think that code should work, you just send the all the objects separated by commas to the print method.

AznTiger @ Tue Mar 11, 2008 7:54 pm wrote:
and (last question, I promise) how would you do it for many different strings in one line?

What do you mean by many strings in one line? The concatenation operator in python is +, as it is in java.
If you are talking about having multiple integers or decimals in a string using the % operator, well I know in ruby you send all arguments in an array :
Ruby:
print "You have %d butterflies and %f nets" % [5, 4.5]

So, that might work in python too.
McKenzie




PostPosted: Wed Mar 12, 2008 8:14 am   Post subject: Re: Help me!

syntax_error has the right answer, assuming I understand the question. Obviously his example was not meant to be useful. Still not useful, but a little better is:
code:
for i in range(10):
    print "~",
wtd




PostPosted: Wed Mar 12, 2008 10:32 am   Post subject: Re: Help me!

McKenzie @ Wed Mar 12, 2008 9:14 pm wrote:
syntax_error has the right answer, assuming I understand the question. Obviously his example was not meant to be useful. Still not useful, but a little better is:
code:
for i in range(10):
    print "~",


Or better yet...

code:
print "~" * 10
McKenzie




PostPosted: Wed Mar 12, 2008 12:00 pm   Post subject: Re: Help me!

The point was not how to make a line. It was why you would ever want to continue outputting on the same line. The only point of my program was to say that he is probably generating solutions in a loop and he wants them displayed on one line.
richcash




PostPosted: Wed Mar 12, 2008 2:21 pm   Post subject: RE:Help me!

Ah, my fault, I was beginning to think python's print method was equivalent to ruby's.

syntax_error is correct then, you need a comma at the end.
wtd




PostPosted: Wed Mar 12, 2008 4:52 pm   Post subject: RE:Help me!

Alright, it seems I'll have to get more creative to satisfy McKenzie. Wink

code:
print "".join("~" for x in range(10)),


I find that imperative loops like the above to print output are often indicative of a missed opportunity to write idiomatic Python code.

Plus, I just enjoy showing people different ways to do things.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: