I wrote the following program:
print ("Mom the food was good:"),
print ("Yummy"* 3)
Based on Python Programming Micheal Dawson the expected result is,
Mom the food was good:YummyYummyYummy
where as the result I get is :
Mom the food was good:
YummyYummyYummy
Why isn't the comma between the 2 prints not suppressing the newsline?
CodePudding user response:
Why isn't the comma between the 2 prints not suppressing the newsline?
Because that was a feature of the Python 2 print statement. Python 3 (which you should be learning) no longer has a print statement, print
is a function, and the commas cannot act like this. Essentially,
print(something),
is a tuple literal, evaluating to (None,)
In Python 3, the print
function accepts sep=' '
and end='\n'
keyword arguments (the defaults shown), which can be used to control this behavior:
print("Hello ", end="")
print("World")
Or:
print("everything", "on", "it's", "own", "line", sep="\n")
CodePudding user response:
print has '\n' in the end,you can use print("hello",end="")
CodePudding user response:
The comma between the operators "print" does not concatenate them. To write code that prints a message in one line, you need to use optional operator "end"
print ("Mom the food was good:", end='')
print ("Yummy" * 3)
Output:
Mom the food was good:YummyYummyYummy