Python int to binary string?

You can use the built-in bin() function to convert an integer to a binary string in Python. Here is an example:

x = 5
binary_string = bin(x)
print(binary_string)

Output:

'0b101'

Watch a course Python - The Practical Guide

Note that the result will include the prefix '0b' indicating that the value is in binary representation. If you want to remove this prefix, you can use string slicing like this:

x = 5
binary_string = bin(x)[2:]
print(binary_string)

Output:

'101'

You can also use the format method to achieve the same

x = 5
binary_string = format(x,'b')
print(binary_string)

Output:

'101'