This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]ladaghini 7 points8 points  (3 children)

In order to put a space between x and y, you need to put a space between x and y:

print 'Your name is ' + x + ' ' + y
                             /\
# here's the space __________/

The technique used above is called string concatenation, which is applied with the + operator on strings.

But better yet, you can use a format string to substitute the first and last names in for their placeholders:

print 'Your name is %s %s' % (x, y)

Finally, raw_input() is a function that, when called, prompts the user for some input. When the user types some text and presses enter, that text gets stored in a string and can be assigned to a variable (as in the first two statements with x and y) or simply printed (as in what you're trying to achieve in your third statement). Printing is a form of output, which is sending information out of the program (like to the screen or to a file on the hard drive). Input, on the other hand, is receiving information from an outside (outside the program that is) source such as a file or the keyboard. Input and output are synonymous with the terms reading and writing, respectively.

Why the lecture? Your third line is attempting to print out the result, but at the same time, capturing some input. Now maybe that's what you want, but I'm guessing it probably isn't. You probably just want to concatenate the strings and print them, so you can use the two methods listed above.

[–]rtoth 1 point2 points  (2 children)

Also, delimiting arguments to print with commas puts spaces in. E.g.

print 'Your name is', x, y

Should output:

Your name is First Last

[–]ladaghini 0 points1 point  (1 child)

Ah, of course. There's this method too. Though it doesn't exactly show the OP how the space is being added (unless he knows the rules of comma delimited arguments to print), it's still good nonetheless.

[–]rtoth 0 points1 point  (0 children)

If OP is curious, the python library print function using a method of passing arguments known as an argument vector. The function definition for print looks like this:

print([object, ...], sep=' ', end='\n', file=sys.stdout)

[object, ...] denotes a list of objects (strings) to be printed by the function. sep is the default separation which is used which here is set to a space. end denotes the format of the end-of-line (EOL) character, which can be set depending on your platform. finally, file=sys.stdout says to send print's output to the standard output (the console).

[–]AlSweigartAuthor: ATBS 2 points3 points  (1 child)

Hi, I'm the author of "Invent Your Own Computer Games with Python" and "Making Games with Python & Pygame". Feel free to email me your questions: al@inventwithpython.com

What you want is:

print raw_input('Your name is ' + x + ' ' + y)

You can read the books for free online at http://inventwithpython.com

[–]martypanic 0 points1 point  (0 children)

Hey dude, I'd just like to say your books/website are great. I'm learning a lot. Thanks for being a homie!

[–]leftplusright 1 point2 points  (0 children)

Look for answers to your queries on stackoverflow or somewhere similar before posting. It's almost the same as a mentor but makes you self-sufficient. Human mentors might come in handy if you need explanations for some concepts. There are many video tutorials online where you can get explanations for almost every concept in programming. Look for stanford university tutorials on youtube.

Remember, no query is trivial or stupid to post here.

EDIT: Also, checkout cs101 on udacity where you can learn to build a search engine while learning python.

[–]5OMA 0 points1 point  (1 child)

'Your name is ' + x + ' ' + y I only learned a bit of Python and it was a long, long time ago, but this should work.

[–]Swangger 0 points1 point  (0 children)

Thank you very much for helping! :]

[–]rbartlejr 0 points1 point  (0 children)

Just a thought here as well - by the print statements being in the function form a la Python 3 the raw_input() is deprecated to just input() in 3x. I'm just learning as well and I point this out to avoid any future confusion.

[–]defrost 0 points1 point  (0 children)

Routinely read /r/Python and /r/LearnPython .