Convert decimal to binary in python



  • 3 Answer(s)

    Using bin(i) for the textual representation of the number in binary:

    >>> bin(10)
    '0b1010'
    >>> 0b1010
    10
    
    Answered on December 28, 2018.
    Add Comment

    Try this one line code:

    "{0:#b}".format(my_int)
    
    
    Answered on December 28, 2018.
    Add Comment

    Check this code with not using the 0b in starting:

    "{0:b}".format(int)
    
    

    Using formatted string literal or f-string and PEP in python 3.6:

    f"{int:b}"
    
    
    Answered on December 28, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.