KnowledgeBoat Logo

Computer Science

Consider the following code :


name = input ("What is your name?")
print ('Hi', name, ',')
print ("How are you doing?")

was intended to print output as

Hi , How are you doing ?

But it is printing the output as:

Hi ,
How are you doing?

What could be the problem ? Can you suggest the solution for the same ?

Python Funda

38 Likes

Answer

The print() function appends a newline character at the end of the line unless we give our own end argument. Due to this behaviour of print() function, the statement print ('Hi', name, ',1) is printing a newline at the end. Hence "How are you doing?" is getting printed on the next line.
To fix this we can add the end argument to the first print() function like this:

print ('Hi', name, ',1, end = '')

Answered By

29 Likes


Related Questions