Sam Liddle
Sam Liddle

Reputation: 168

Equivalent of python2 chr(int) in python3

# python2
print(chr(174))
?

# python3
print(chr(174))
®

I'm looking for the equivalent of chr() from python2. I believe this is due to python 3 returning unicode characters rather than ASCII.

Upvotes: 6

Views: 5010

Answers (2)

Little Roys
Little Roys

Reputation: 5789

Actually, in Py3 chr is equivalent of unichr in Py2. You can use bytes or bytearray.

For example:

>>> print(bytes([174]))
b'\xae'

or

>>> print(bytearray([174]))
bytearray(b'\xae')

b'\xae' equals to ?

Upvotes: 6

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96246

I believe that this would be the closest equivalent:

>>> print(chr(174).encode('ascii', errors='replace'))
b'?'
>>>

Upvotes: 2

Related Questions