0%

🪄 The Binary Whisper

It’s not about 1s and 0s. It’s about meaning hidden in order. Python just knows how to listen.
🔮 int(‘101’, 2)

A string of ones and zeros. To the untrained eye, just digits. But beneath the surface, a number whispers. A value waiting to be heard. Python leans close and listens. The int() function with a base argument translates binary strings into integers. The first argument is the string. The second is the base (2 for binary, 8 for octal, 16 for hexadecimal). Python decodes the pattern and returns the human readable number.

🕯️ Magic Note

Binary is base 2. Each digit represents a power of two. The rightmost digit is 2⁰ (1), the next is 2¹ (2), then 2² (4), and so on. The string “101” means (1 × 4) + (0 × 2) + (1 × 1) = 5. int(‘101’, 2) performs this calculation and returns 5. The same function works for any base from 2 to 36.

The syntax int(‘101’, 2) takes the binary string ‘101’ and interprets it in base 2. The result is the integer 5. You can do the same with octal (base 8) like int(’12’, 8) which returns 10, or hexadecimal (base 16) like int(‘A’, 16) which returns 10.
  • The base parameter can be any integer from 2 to 36
  • Binary strings must contain only 0 and 1 characters
  • Case insensitive for bases above 10, ‘A’ and ‘a’ both mean 10
  • Raises ValueError if the string contains invalid digits for the base
💡 Use bin(x) to convert an integer back to a binary string. Use oct(x) for octal and hex(x) for hexadecimal. For binary literals in code, you can write 0b101 directly, which equals 5. This is often clearer than using int() for hardcoded values. Use int() when the binary string comes from user input or external data.
CallBaseResult
int(‘101’, 2)Binary (base 2)5
int(’12’, 8)Octal (base 8)10
int(‘A’, 16)Hexadecimal (base 16)10
int(‘1111’, 2)Binary15
int(‘FF’, 16)Hexadecimal255
int(‘101’, 3)Ternary (base 3)10
⚠️ The base parameter must be an integer between 2 and 36. A base of 1 or above 36 raises a ValueError. Also, the string cannot contain a sign (‘+’ or ‘-‘) when a base is specified. For negative binary numbers, convert the absolute value first then apply the negative sign. The string is case sensitive only in that letters must be within A-Z or a-z, but the case itself does not affect the value.
Examples

Python

# Converting binary string to integer

binary_string = “101”

number = int(binary_string, 2)

print(number)

# Output: 5

print(type(number))

# Output:

Python

# Working with different bases

binary = int(“1101”, 2)

octal = int(“17”, 8)

hexadecimal = int(“1A3F”, 16)

ternary = int(“210”, 3)

print(f”Binary 1101 = {binary}”)

print(f”Octal 17 = {octal}”)

print(f”Hex 1A3F = {hexadecimal}”)

print(f”Ternary 210 = {ternary}”)

# Output: Binary 1101 = 13

# Output: Octal 17 = 15

# Output: Hex 1A3F = 6719

# Output: Ternary 210 = 21

Python

# Converting back to binary string

number = 42

binary = bin(number)

octal = oct(number)

hexadecimal = hex(number)

print(f”42 in binary: {binary}”)

print(f”42 in octal: {octal}”)

print(f”42 in hexadecimal: {hexadecimal}”)

# Output: 42 in binary: 0b101010

# Output: 42 in octal: 0o52

# Output: 42 in hexadecimal: 0x2a

Common Mistakes
  • Forgetting the base argument, writing int(‘101’) which returns 101, not 5
  • Using invalid digits for the base, like int(‘102’, 2) (2 is not valid in binary)
  • Passing a binary literal like int(0b101) which already gives 5, making the base unnecessary

⚡ Whisper

The pattern hides beneath the surface. Ones and zeros arranged in sacred order. Python leans close and listens. The meaning emerges. A whisper becomes a number. Order becomes value. The binary speaks, and Python understands.