[Python] lstrip method does not produce what I want

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to convert decimal to binary number and remove the 0b on the left most side so I can work with 1's and 0's. I succeeded however, when the binary was actually 0. if you lstrip "0b" from "0b0" must be "0" right?

    But there is no character inside. it's empty as you can see in the last line of output.

    What is going on???

    Source code:

    value = [255, 255, 255, 0]
    binary = [bin(x) for x in value]
    binstring = [str(y) for y in binary]
    purebin = []
    for x in binstring:
    purebin.append(x.lstrip("0b"))
    print(binary)
    print(binstring)
    print(purebin)


    Output:


    ['0b11111111', '0b11111111', '0b11111111', '0b0']

    ['0b11111111', '0b11111111', '0b11111111', '0b0']

    ['11111111', '11111111', '11111111', '']
    ​

    Continue reading...

Compartilhe esta Página