I want to convert binary strings to base-10 digits. e.g. '1010' to 10 or '11' to 3.

Is there any python function to do this?

Is there any python function to do this?

+2 votes

Best answer

The built-in function **int() **can be used to convert a binary string to a base-10 digit. This function has a parameter "base." You need to set it to 2 to tell the function that the string you have provided is binary.

Here are some examples:

>>> int('10',base=2)

2

>>> int('1011',base=2)

11

>>> int('11',base=2)

3

>>> int('1010',base=2)

10