This question already has an answer here:
If I'd like to put some input in between a text in python, how can I do it without, after the user has input something and pressed enter, switching to a new line?
E.g.:
print "I have"
h = input()
print "apples and"
h1 = input()
print "pears."
Should be modified as to output to the console in one line saying:
I have h apples and h1 pears.
The fact that it should be on one line has no deeper purpose, it is hypothetical and I'd like it to look that way.
While the other answer is correct, the
%
is deprecated, and the string.format()
method should be used instead. Here's what you could do instead.Also, from your question it's not clear as to whether you're using python2.x or python3.x, so here's a python3.x answer as well.
If I understand correctly, what you are trying to do is get input without echoing the newline. If you are using Windows, you could use the msvcrt module's getwch method to get individual characters for input without printing anything (including newlines), then print the character if it isn't a newline character. Otherwise, you would need to define a getch function:
You can do following:
Basically you have one string that you formant with two inputs.
Edit:
To get everything on one line with two inputs is not (easily) achievable, as far as I know. The closest you can get is:
Which will output:
The comma notation prevents the new line after the print statement, but the return after the input is still there though.