Skip to content
← Back to Community
String function
Profile icon
ChamilaSangakar

Example_2

str = '100'

print("int('100') with base 2 = ", int(str, 2))
print("int('100') with base 4 = ", int(str, 4))
print("int('100') with base 8 = ", int(str, 8))
print("int('100') with base 16 = ", int(str, 16))

Output :

int('100') with base 2 = 4
int('100') with base 4 = 16
int('100') with base 8 = 64
int('100') with base 16 = 256

Hi

I just started learning python language and came across this example which I don't understand how the output is achieved for the above mentioned example. (4,16,64,256)

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
ANGELOPEREZ
Profile icon
SelenJakop
Profile icon
ChamilaSangakar
Comments
hotnewtop
Profile icon
Coder100

Hello, this is something with bases. We are using base 10, but that choice is arbitrary, so there are other ways to "count". What you are seeing are the other ways to "count" of expressing 100 in base 10.
100 = 10 * 10
2 base 2 = 10 base 10, so 2 * 2 = 4 = 100
4 base 4 = 10 base 10, so 4 * 4 = 16 = 100
8 base 8 = 10 base 10, so 8 * 8 = 64 = 100
16 base 16 = 16 base 16, so 16 * 16 = 256

And there you go.

Sorry if this confuses you, but this is a really mathematical thing. Search it up on bing or google to learn more.

Please mark as correct answer if it has helped you!