I am trying to convert an integer to bytes
Can you give more detail here, or what the end goal is? Do you mean the text of an integer (perhaps for passing to an external device), or some type of encoding of an integer value?
In your first example you’re creating a bytes object from the ASCII character
1. The bytes object holds the ASCII value and the ASCII character is printed. But the byte value inside is 49, not 1. These objects are equal:
>>> int.from_bytes(b'1', "big")
In your second example, you’re passing an integer into bytes(). But from the help we can see what is expected.
| bytes(int) -> bytes object of size given by the parameter initialized with null bytes
This says the integer sets the size of the object that is created, not the content within. I don’t understand why your example shows \x01. It should be \x00.
>>> x = 5
The object is made up of 5 null bytes.
So for how to turn an integer into bytes depends on what you want. Do you want each byte in the object to be an integer value less than 256, or do you want to encode a potentially large int into a number of bytes, or do you want to encode a string representing an integer into bytes?
If you want the first one, the pass in the integers as an iterable (such as a list), even if there’s only one value.
>>> bytes([1, 2, 3, 49, 70])