0

I have the following piece of code:

test = "é".encode('utf-8') print(test) 

Now, this would give us: b'\xc3\xa9', as expected. Now I would actually have "\xc3\xa9" as a string. How can I achieve this?

I looked at encoding and decoding methods in Python, but unfortunately they do not result in the desired outcome.

3
  • 1
    Your requirement seems extremely dubious but decoding it as CP1252 even though it isn't really will achieve what you are asking. The vast majority of scenarios where somebody wants to do this, they are doing something very wrong. Why do you want this? Commented Jan 31, 2018 at 10:18
  • 1
    Or are you looking for repr(test)? Commented Jan 31, 2018 at 10:19
  • I think I am looking for repr(test) indeed. Thanks! Commented Jan 31, 2018 at 10:28

1 Answer 1

1

you can use both repr() or str()

# -*- coding: utf-8 -*- test = "é".encode('utf-8') print(test) # using repr() my_string = repr(test)[2:-1] print(my_string) # using str() my_string = str(test)[2:-1] print(my_string) 

output:

b'\xc3\xa9' \xc3\xa9 \xc3\xa9 

Just a little background to this.

The repr() function will call the test.__repr__() method of the bytes object test. And the str() function will call the test.__str__() method of the bytes object test, if __str__() is defined, else it will call the __repr__() method instead.

This can easily be seen, consider this code:

class MyClass(object): def __init__(self): pass def __repr__(self): return 'repr' def __str__(self): return 'str' m = MyClass() print(str(m)) print(repr(m)) 

output:

str repr 

if there is no .__str__() defined, consider the following code:

class MyClass(object): def __init__(self): pass def __repr__(self): return 'repr' #def __str__(self): # return 'str' m = MyClass() print(str(m)) print(repr(m)) 

output:

repr repr 

More information about __str__() and __repr__() can be found in the Datamodel documentation

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.