I have 2 codes snippets in Python 3:
print(b'3'.rjust(4, b'0'))
b'0003'
length = str(len("str")).encode() # length as bytes
length.rjust(4, b'0') # trying to pad length
print(length)
b'3'
As I understand it, both should produce the same result as the first snippet. Why does the second snippet not work as expected?
CodePudding user response:
length.rjust
returns a new string. Strings in Python cannot be modified in place. Try:
length = length.rjust(4, b'0')