3

I have the following code (Assuming I am typing in IDLE line by line)

# -*- coding: utf-8 -*-
s = u"My Currency is - £"
s
print s

for - s - I'm getting an output - u'My Currency is - \xa3'

for - print s - I'm getting an output - u'My Currency is - £'

What is the difference ? Why I am getting different outputs ?

Kartheek Palepu
  • 972
  • 8
  • 29

2 Answers2

4

In Python, print shows the result of __str__ on its arguments, which may be different from __repr__. For more, see here: Difference between __str__ and __repr__ in Python

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Not exactly, try calling explicit `str(s)` or `s.__str__()` and You'll get `UnicodeEncodeError: 'ascii' codec can't encode characters...`, but `print` works fine, so `print` makes something more. – user2622016 Dec 16 '15 at 10:38
0

it is beacause of differences between

__str__

and

__repr__.

. diff. str and repr

Community
  • 1
  • 1
arash javanmard
  • 1,362
  • 2
  • 17
  • 37