Ad
How Can I Set The Number Of Bytes To Be Displayed By The Binary Form Of A Character In Python?
I want to be able to set the number of bytes displayed by each character in the string to be 6. Here, the string is jJ
and I'm getting the output as 1000111001
. J
is giving 1001
and I want it to display 001001
instead.
a = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/']
def mapFirst(string):
return ''.join(bin(ord(chr(a.index(c)))) for c in string).replace('0b','')
def main():
k = 'jJ'
print(mapFirst(k))
if __name__ == "__main__":
main()
Ad
Answer
Change:
return ''.join(bin(ord(chr(a.index(c)))) for c in string).replace('0b','')
With:
return ''.join(str(bin(ord(chr(a.index(c))))).replace('0b','').zfill(6) for c in string)
The function you were looking for is zfill(width)
Note: I suggest you to split that line into multiple lines for readability purposes.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad