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?
>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.