KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I wanted to pad a string with null characters ("\x00"). I know lots of ways to do this, so please do not answer with alternatives. What I want to know is: Why does Python's string.format() function not allow padding with nulls? Test cases: >>> "{0:\x01<10}".format("bbb") 'bbb\x01\x01\x01\x01\x01\x01\x01' This shows that hex-escaped characters work in general. >>> "{0:\x00<10}".format("bbb") 'bbb ' But "\x00" gets turned into a space ("\x20"). >>> "{0:{1}<10}".format("bbb","\x00") 'bbb ' >>> "{0:{1}<10}".format("bbb",chr(0)) 'bbb ' Even trying a couple other ways of doing it. >>> "bbb" + "\x00" * 7 'bbb\x00\x00\x00\x00\x00\x00\x00' This works, but doesn't use string.format >>> spaces = "{0: <10}".format("bbb") >>> nulls = "{0:\x00<10}".format("bbb") >>> spaces == nulls True Python is clearly substituting spaces ( chr(0x20) ) instead of nulls ( chr(0x00) ).
Tags (comma-separated)
Save Edits
Cancel