'Why does the rjust method leave a string unchanged?
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?
Solution 1:[1]
length.rjust returns a new string. Strings in Python cannot be modified in place. Try:
length = length.rjust(4, b'0')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
