I’m trying to convert a string into a binary integer:
string = "0b011"
i = int(string)
But this code raises a ValueError. However, the following code works fine:
i = int(0b011)
But here I’ve passed a binary literal, not a string. How do I convert a string?
Try this code:
It uses the built-in procedure int() with the optional base parameter, which indicates the base to be used in the conversion – two in this case, from the documentation: