Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: "Print" and "Input" in one line [duplicate]

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.

like image 461
Yinyue Avatar asked Dec 24 '22 19:12

Yinyue


1 Answers

You can do following:

print 'I have %s apples and %s pears.'%(input(),input())

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:

print 'I have',
a=input()
print 'apples and',
p=input()
print 'pears.'

Which will output:

I have 23
apples and 42
pears.

The comma notation prevents the new line after the print statement, but the return after the input is still there though.

like image 71
Wouter Avatar answered Dec 28 '22 06:12

Wouter