Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python print "hello world" vs "hello world"

Tags:

python

>>> print "hello world"
hello world
>>> "hello world"
'hello world'
>>>

What is the difference?

The Python Hello, World! example mostly uses:

print "hello world"

Can I strip that print and just use "Hello world" for giving a Python introduction?

like image 552
limovala Avatar asked Dec 11 '13 04:12

limovala


People also ask

What is the difference between print Hello World and print Hello World?

The difference is that print calls str whereas the default action of the REPL (read evaluate print loop) is to call repr on the object unless it is None .

What is the difference between hello and hello in Python?

Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello".

How do you change Hello World to Hello World in Python?

Source Codeprint('Hello, world! ') Hello, world! In this program, we have used the built-in print() function to print the string Hello, world! on our screen.

What does print Hello World do in Python?

Python Hello world program uses the print function to print hello world. That will be visible on the output screen. The basic way to produce an output in python programming is by using the print() function, where you can pass none or more expressions separated by commas.


2 Answers

The difference is that print calls str whereas the default action of the REPL (read evaluate print loop) is to call repr on the object unless it is None.

Note that if you aren't working in the interactive interpreter (you're not in the REPL), then you won't see any output in the version without print.

Also note that there is a difference between the output. repr adds quotes on strings.

like image 132
mgilson Avatar answered Oct 12 '22 02:10

mgilson


If you substitute the space for a newline, you'll see they don't even really work the same in the REPL.

>>> print "hello\nworld"
hello
world
>>> "hello\nworld"
'hello\nworld'

If you try to use

"hello\nworld"

by itself in a program, you will get no output of course

like image 43
John La Rooy Avatar answered Oct 12 '22 01:10

John La Rooy