+2 votes
in Programming Languages by (26.1k points)
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?

1 Answer

+2 votes
by (33.0k points)
selected by
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)


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


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


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


Related questions