R.Brahmbhatt R.Brahmbhatt - 8 months ago 30
Python Question

Why this single quotes and braces in output in python?

I am using ipython notebook in ubantu version 16.04 and I run this code,

word = 'Rushiraj'
length = 0
for char in 'rushiraj':
length = length + 1
print('There are', length,'character')

I get this output:
('There are', 8, 'character')

What is the reason for this single quotes and round braces in output ?It should not be there !


The output you are seeing is due to the fact that you are using Python 2, but you're using the print syntax from Python 3. In Python 3, print is a function and takes arguments like other functions (as in print(...)).

In Python 2, print is a statement, and by using parentheses you are actually passing it a tuple as its first argument (so you are printing out the Python representation of a tuple).

You can fix this in two ways.

If you add from __future__ import print_function to the top of your file, then print will behave like it does in Python 3.

Alternately, you can call it like:

print 'There are', length,'character'