Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what encoding is b'' using by default in python?

when i use b'' to convert string to bytes, I don’t have to think about encoding type.

However if the string contains variable, for example s = f'this is {x}', I can’t convert s to bytes with b''. I must use bytes() but then I have to think about encoding type.

Could someone please confirm my understanding above? and if it’s correct, what encoding is b'' using by default?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

b'...' always uses ASCII, because only (certain) ASCII characters are allowed in bytes literals. In such a literal, an ASCII character represents its ASCII value as a byte. b'a' and b'\x61', for example, are equivalent, producing the same value as bytes([0x61]).

You aren’t converting str values to bytes values; there is just overlap between the characters that can appear in each.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading