0

I started learning Python recently and while using lists I faced this problem.

Here I assigned some movie names to variable movies:

>>> movies = ["dragon","dragon warrior","kungfu","kungfu panda"]

after assigning, I used the print command to execute it:

>>> print(movies)

the output is as below:

['dragon', 'dragon warrior', 'kungfu', 'kungfu panda']

In the same way, when I started using a for-loop in lists with the same print command, the output is entirely different. I can't understand how it changed because of the for-loop:

>>> for fav_movie in movies:
          print(fav_movie)

output:

dragon
dragon warrior
kungfu
kungfu panda

Why is the same print command working differently?

  • It sounds like you don't understand what the `for` loop does, which is a more fundamental issue. It might be easier to craft an answer that makes sense to you if we knew how far are you into your learning (because it's very easy to assume an intelligent novice understands a lot more than he actually does). – abarnert Aug 15 '13 at 18:52
  • (guess)Do you refer to the fact that before it was printed `'dragon'` and afterwards it is printed only `dragon`(without delimiters)? You should look at the difference between `str` and `repr`(try `print(str("A"))` and `print(repr("A"))`). When doing `print(movies)` the `print` function calls `str(movies)` to obtain a string representation of the list. `str(movies)` calls `repr(movie)` for each movie and creates the final result. Calling `repr` on a string produces a string containing the delimiters, hence what you see. – Bakuriu Aug 15 '13 at 18:55

6 Answers6

2

print(movies) prints the str version of the list object, while in the loop you're printing the str version of individual items inside the list.

>>> print(movies)
['dragon', 'dragon warrior', 'kungfu', 'kungfu panda']

is equivalent to:

>>> print(movies.__str__())   #or simply print(str(movies))
['dragon', 'dragon warrior', 'kungfu', 'kungfu panda']

Note that you're seeing quotes here in the list elements, that's because the representation used is the repr version of those objects. The repr version of the list would also contain quotes around it.

>>> repr(movies)
"['dragon', 'dragon warrior', 'kungfu', 'kungfu panda']"

repr is a computer friendly output and str is human friendly, a good example would be floats:

>>> x = 1.1 + 2.2
>>> print repr(x)
3.3000000000000003
>>> print str(x)
3.3

Also note that the string returned by repr can be re-used to generate that object using eval.

For more details read: Difference between str and repr in Python.

Inside the loop you're printing the str version of each item:

>>> print(movies[0])
dragon
>>> print(movies[0].__str__())
dragon
>>> for movie in movies:
...     print(str(movie))
...     
dragon
dragon warrior
kungfu
kungfu panda
Community
  • 1
  • 1
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

The first command you're printing the actual list object, the second you're looping through the list's elements and printing one by one.

Doktoro Reichard
  • 577
  • 1
  • 7
  • 22
1

The first print statement is printing a list; while the second one is printing strings.

mshsayem
  • 17,557
  • 11
  • 61
  • 69
1

That's how Python works. Using print(movies) prints a string representation of the whole list movies where as:

for fav_movie in movies:
      print(fav_movie)

prints the items in movies one at a time.

>>> l = [1, 2, 3]
>>> print(l)
[1, 2, 3]
>>> for i in l: print(i)
...
1
2
3
>>>
1

In addition to iCodez answer, it is worth noting that print will introduce a newline character. For example:

>>> import sys
>>> l = ["a", "b", "c"]
>>> print l
['a', 'b', 'c']
>>> for i in l: print i
... 
a
b
c
>>> for i in l: sys.stdout.write(i)
... 
abc>>> 
bitfish
  • 468
  • 1
  • 4
  • 7
0

print(x) does the following:

1.) If x is not a string, convert it to such by calling x.__str__().

>>> 'Hello!'.__str__()
'Hello!'
>>> [1,2,3].__str__()
'[1, 2, 3]'

2.) Output that string

3.) Start a new line (you actually can turn that off).


print(movies) will actually do print(movies.__str__()), while

for movie in movies:
    print(movie)

will go through the movies, and print each movie separately. Since the movies are already strings, no conversion step is neccessary. And every movie gets its own line due to step 3.

Markus Unterwaditzer
  • 7,992
  • 32
  • 60