0

I am trying to implement left padding of variable length of a string with desired fill parameter with the simplest logic if possible, even a pre-built function. e.g: 123 as dd123 where d is the fill parameter.[" " in this case] As I found out from some sources this can be done via zfill.But it pads the string with left zeros.. But I want space padded string.

Again space" " padding can be done via '{:>10}'.format('test')-Still it is eligible only when the fill amount is hardcoded integer -as '{:>x}'.format('test') didn't work where x is the variable predefined with an integer value..

So where is the combined solution of those 2 problems?

Thanks for your time.

1 Answer 1

1

It can be done easily by following line of code-

s.rjust(fill_width,fill_parameter)

where s is the string,you can put the VARIABLE no. of bit-places you want your string to occupy in total and put fill-parameter as " "

As example you can see

str = "hello world";
print str.rjust(15, " ")

this code will result in

    hello world
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.