rjust( ) Function
The rjust( ) Function is used to right justify a String using Space by default.
x = "Hello" y = x.rjust(10) print(y)
In the above code, we have declared a String 'Hello' and assigned it to a variable 'x'.
Then we have created a variable 'y' that will reserve '10' blocks. And right justified the String 'Hello' using 'rjust( )' Function.
That shifts the String Hello towards the right filling the first 5 locations with space (i.e. ' ').
Now, let us say you want the fill the left blocks with something other than spaces.
And luckily, Python provides a second parameter that can be used as a padding character.
Let us see with the below example.
x = "Hello" y = x.rjust(10, "*") print(y)
In the above code, we have declared a String 'Hello' and assigned it to a variable 'x'.
Then we have created a variable 'y' that will reserve '10' blocks. And right justified the String 'Hello' using 'rjust( )' Function.
That shifts the String Hello towards the right filling the first 5 locations with asterisk (i.e. '*').