
-----------------------------------
AznTiger
Sun Mar 09, 2008 6:34 pm

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?

-----------------------------------
richcash
Sun Mar 09, 2008 7:00 pm

RE:Help me!
-----------------------------------
I have never used python (... yet), but you are looking for the print command I think. Shouldn't this work :

print "a", 1, "b"

-----------------------------------
AznTiger
Sun Mar 09, 2008 7:05 pm

RE:Help me!
-----------------------------------
Kay thanks. I've tried everything but that lol and yes it does work

-----------------------------------
wtd
Sun Mar 09, 2008 10:23 pm

RE:Help me!
-----------------------------------
Not as nice as it could be:

print "You have %d butterflies" % 10

-----------------------------------
AznTiger
Mon Mar 10, 2008 3:17 pm

RE:Help me!
-----------------------------------
alright thanks

-----------------------------------
AznTiger
Mon Mar 10, 2008 6:58 pm

RE:Help me!
-----------------------------------
Now how would you do that for floats instead?

-----------------------------------
richcash
Mon Mar 10, 2008 8:20 pm

RE:Help me!
-----------------------------------
I believe it is %f instead of %d.

[url=http://docs.python.org/lib/typesseq-strings.html]Here are some others.

-----------------------------------
AznTiger
Tue Mar 11, 2008 7:54 pm

RE:Help me!
-----------------------------------
and (last question, I promise) how would you do it for many different strings in one line?

-----------------------------------
syntax_error
Tue Mar 11, 2008 10:00 pm

Re: Help me!
-----------------------------------
never used python but based on the above example and google 

here 
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
Wed Mar 12, 2008 12:42 am

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.

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 :
print "You have %d butterflies and %f nets" % 
So, that might work in python too.

-----------------------------------
McKenzie
Wed Mar 12, 2008 8:14 am

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:
for i in range(10):
    print "~",

-----------------------------------
wtd
Wed Mar 12, 2008 10:32 am

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:
for i in range(10):
    print "~",

Or better yet...

print "~" * 10

-----------------------------------
McKenzie
Wed Mar 12, 2008 12:00 pm

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
Wed Mar 12, 2008 2:21 pm

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
Wed Mar 12, 2008 4:52 pm

RE:Help me!
-----------------------------------
Alright, it seems I'll have to get more creative to satisfy McKenzie.  ;)

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.

-----------------------------------
AznTiger
Wed Mar 12, 2008 5:19 pm

RE:Help me!
-----------------------------------
Thanks guys, you have no idea how much easier that made life

-----------------------------------
AznTiger
Tue Mar 18, 2008 8:10 pm

RE:Help me!
-----------------------------------
Alright so I know how the comma works now, but everytime i use it in a print command
i.e
print "A", "B"
the result becomes A B instead of AB so does anyone know how to solve this problem?
Thanks again

-----------------------------------
wtd
Tue Mar 18, 2008 8:34 pm

RE:Help me!
-----------------------------------
Don't use "print".  :)

Use the stdout object from the module sys.  Specifically the write function.  You may also need the flush function is line buffering is present.

-----------------------------------
AznTiger
Tue Mar 18, 2008 9:00 pm

RE:Help me!
-----------------------------------
So how would this work in the actual code?

-----------------------------------
wtd
Wed Mar 19, 2008 9:50 am

RE:Help me!
-----------------------------------
You tell me?  

1.  How do you import a module?
2.  How do you access an object in that module?
3.  How do you call a function (method, if you prefer) on an object?
