Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 15 Online
OpenStudy (anonymous):

Hello all, I just got started with the course and just completed Problem Set 0. I didn't have any issues solving the set, but I have a Python question relating to my code: How can I print multiple variables or strings on the same line ie not proceeding to a newline? I just used + and defined a space value to provide a space between the variables/strings printed. I'm sure there must be a better way of doing this... I know \n creates a newline, does /n prevent a newline? The code: http://codepad.org/DaXclMVw PS I am using Python 2.5.4

OpenStudy (anonymous):

Sorry for the double post. I think I've answered much of my own question: Updated code: http://codepad.org/KmQJebO6 I realized I was using double quotes (") instead of single quotes ('), also realized I did not need to define the commaspace string, and that I was outputting the lastname and firstname variables in the incorrect order (at least as far as the problem set asked for them). It also now appears that there is no other way to print multiple strings or variables on the same line other than using + to concatenate them(?).

OpenStudy (anonymous):

There isn't a difference between single quotes and double quotes in Python. Other than that, try print foo, bar, baz This will print foo, bar, and baz on a single line, and then a new line. The comma indicates for the interpreter to print "something ", rather than "something\n"

OpenStudy (anonymous):

@t.kovacs- (use commas) y = there print('hi', y)

OpenStudy (anonymous):

For the sample program you wrote: y = 'there' print('hi',y) Prints: ('hi', 'there') Same if I alter my code for the Name program: http://codepad.org/xKTwe4Lg Is there a way to use commas ie. truncating things without the parens, single quotes, and comma? Also, concatenating a string and a trunc is a syntax error, right? So, fullname = firstname,lastname print 'Your name is: '+fullname outputs a syntax error message.

OpenStudy (anonymous):

@t.kovacs You are creating a tuple during your fullname statement. That is why it is printing the output like (firstname, lastname) Use this instead, fullname = firstname + ' ' + lastname *FYI it is pretty standard for local variable names to follow the lower_case_with_underscores naming convention. http://www.python.org/dev/peps/pep-0008/

OpenStudy (anonymous):

Ah, ok thanks. Your solution is exactly what I used for my first successful attempt, I was curious if there was another way to do the same thing. Thanks for the heads up on the convention on using underscores in variable names.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!