delete 0b from binary number in python

To delete the 0b prefix from a binary number in Python, you can convert it to a string and then slice the string to remove the first two characters. Here's an example:

main.py
binary_num = 0b101010
binary_str = bin(binary_num)[2:]  # convert to binary string and slice to remove '0b'
print(binary_str)  # prints '101010'
145 chars
4 lines

In the above code, we first define a binary number binary_num using the 0b prefix. We then convert it to a binary string using the built-in bin() function. This function adds the 0b prefix to indicate that the string represents a binary number.

To remove the prefix, we slice the string using the [2:] syntax. This means we start at the third character (index 2) and include all characters after that. The resulting string, binary_str, does not contain the 0b prefix.

Finally, we print the binary string to confirm that the prefix has been removed.

gistlibby LogSnag