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

前端 未结 9 1857
日久生厌
日久生厌 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:24

    http://docs.python.org/library/string.html#string.replace

    Replace all the 1's with 2's, then replace the 0's with 1's, finally replacing the 2's with 0's.

    "10011".replace("1", "2").replace("0", "1").replace("2", "0")
    

提交回复
热议问题