Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
confusion on print function
#1
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)?
Reply
#2
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.
Reply
#3
(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"
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 304 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Print confusion matrix MrSonoa 1 918 Apr-20-2023, 04:17 PM
Last Post: farshid
  How to print variables in function? samuelbachorik 3 915 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  How to print the output of a defined function bshoushtarian 4 1,313 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Why does absence of print command outputs quotes in function? Mark17 2 1,387 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  return vs. print in nested function example Mark17 4 1,749 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  output correction using print() function afefDXCTN 3 11,099 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print function output wrong with strings. mposwal 5 3,127 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Output with none, print(x) in function Vidar567 3 2,522 Nov-24-2020, 05:40 PM
Last Post: deanhystad
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,493 Aug-10-2020, 02:51 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020