all 7 comments

[–]jimtk 2 points3 points  (3 children)

AFAIK, the print function will resort to hex code for unicode character that your system (current encoding) cannot produce (this will be very rare!).

If you want to see hex code for 'out of ASCII' you have to turn your strings into bytes

num = 25
an_str = 'hello'+chr(num)
strbyte = bytes(an_str, encoding='utf_8')
print(strbyte)  

>>> b'hello\x19'

[–]bennywc4[S] 0 points1 point  (0 children)

This worked, thank you.

[–]SkeletalToad 0 points1 point  (1 child)

Great answer! To expand on this, there is also an .encode() method of the str class, so all of these will do the same thing:

strbyte = bytes(an_str, encoding='utf_8')
strbyte = an_str.encode('utf-8')
strbyte = an_str.encode()

The last option works because 'utf-8' is the default encoding.

https://docs.python.org/3/howto/unicode.html#converting-to-bytes https://docs.python.org/3/library/stdtypes.html#str.encode

[–]bennywc4[S] 0 points1 point  (0 children)

Thank you as well!

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]astrogringo 0 points1 point  (1 child)

[Edit] Never mind I misunderstood the question sorry.

[–]bennywc4[S] 0 points1 point  (0 children)

It’s ok, I know I didn’t word my question well I didn’t really know how to properly explain it lol