Python Forum

Full Version: confusion on print function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
print("Hello");
prints "Hello"

but

print("Hello",3);
prints ('Hello',3)

what happens when we use two arguments like above in print function ? why it prints ('Hello',3)?
In python 2, print is a statement, not a function.

In your first code, you're passing ("Hello") to it, which is the same as "Hello" (just in parentheses).
In the second code, you're passing ("Hello", 3) to it, which is a tuple.

Also, the ; is unnecessary and ugly.
(Mar-10-2018, 04:52 PM)stranac Wrote: [ -> ]In python 2, print is a statement, not a function.

I'm using python 3.4. What is print in Python 3.4 ? statement or function ?

Quote:In the second code, you're passing ("Hello", 3) to it, which is a tuple.

when you say passing ..it reminds me of passing arguments in a function. I see this as print function is taking two arguments here "Hello' and "3"
(Mar-11-2018, 03:45 AM)volcano Wrote: [ -> ]I'm using python 3.4. What is print in Python 3.4 ? statement or function ?
In all python 3 versions is print a function.
(Mar-10-2018, 04:27 PM)volcano Wrote: [ -> ]why it prints ('Hello',3)?
Because you use Python 2.
# Python 2.7
>>> print("Hello", 3)
('Hello', 3)

# Python 3.6
>>> print('hello', 3)
hello 3
You should use Python 3.6.
Help on print function.
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

# Example want to keep comma
>>> print('hello', 3, sep=',')
hello,3