Python - Flipping Binary 1's and 0's in a String

前端 未结 9 1865
日久生厌
日久生厌 2020-12-03 15:52

I\'m trying to take a binary number in string form and flip the 1\'s and 0\'s, that is, change all of the 1\'s in the string to 0\'s, and all of the 0\'s to 1\'s. I\'m new t

9条回答
  •  时光取名叫无心
    2020-12-03 16:26

    Another way to do it is with string.translate() and string.maketrans()

    from string import maketrans
    bitString = "10101010100011010"
    flippedString = bitString.translate(maketrans("10","01"))
    

提交回复
热议问题