The simplest way is to use slicing and str.zfill
function, like this
data = "abcd"
print data[:20].zfill(20) # 0000000000000000abcd
When data
is abcdefghijklmnopqrstuvwxyz
, the output is
abcdefghijklmnopqrst
Note: If you really meant, appending zeros, you can use str.ljust
function, like this
data = "abcdefghijklmnopqrstuvwxyz"
print data[:20].ljust(20, "0") # abcdefghijklmnopqrst
data = "abcd"
print data[:20].ljust(20, "0") # abcd0000000000000000
The advantage of using ljust
and rjust
is that, we can use arbitrary fill character.