TransWikia.com

In Python3 the behaviour of the inbuilt repr() function has changed when compared to Python2

Stack Overflow Asked by Bob Rathbone on February 21, 2021

In Python3 the behaviour of the inbuilt repr() function has changed when compared to Python2.

In python2

repr("Дует Світязь")
"'\xd0\x94\xd1\x83\xd0\xb5\xd1\x82 \xd0\xa1\xd0\xb2\xd1\x96\xd1\x82\xd1\x8f\xd0\xb7\xd1\x8c'"

In Python3

repr("Дует Світязь")
"'Дует Світязь'"

How can I get repr() to work in Python3 the same as in Python2 or as an alternative another method to convert a string to escaped characters
In my code. I use the escaped characters in lookup tables for my LCD display translation routines.

The platform is Raspberry Pi OS(Raspbian).

I have also tried using reprlib() without success. It gives the same output as repr(). Using string.encode goes some way to proving a solution, but the result is different to Python2 repr()

txt = "Дует Світязь"
txt.encode('utf8')
b'xd0x94xd1x83xd0xb5xd1x82 xd0xa1xd0xb2xd1x96xd1x82xd1x8fxd0xb7xd1x8c'

2 Answers

My thanks to Andrej Kesely who put me on the right path for a solution

In Python2

s = repr(text)

In Python3

s = str(text.encode('utf-8')).lstrip('b')

I don't really understand why the authors of Python3 felt the need to change how a built in function such as repr() works. No doubt they have their reasons.

Answered by Bob Rathbone on February 21, 2021

You can encode the string as bytes object:

print('Дует Світязь'.encode('utf-8'))

Prints:

b'xd0x94xd1x83xd0xb5xd1x82 xd0xa1xd0xb2xd1x96xd1x82xd1x8fxd0xb7xd1x8c'

EDIT:

s = str('Дует Світязь'.encode('utf-8')).lstrip('b').replace('\', r'\')
print(s)

Prints:

'\xd0\x94\xd1\x83\xd0\xb5\xd1\x82 \xd0\xa1\xd0\xb2\xd1\x96\xd1\x82\xd1\x8f\xd0\xb7\xd1\x8c'

Answered by Andrej Kesely on February 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP