The ljust() Function is used to left justify a String using Space by default.
x = "Hello" y = x.ljust(10) print(y,"World")
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 left justified the String 'Hello' using 'ljust( )' Function.
That shifts the String Hello towards the left filling the last 5 locations with space (i.e. ' ').
And if you see the print statement,
There is a gap of 5 spaces between 'Hello' and 'World'.
Now, let us say you want the fill the right 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.ljust(10, "*") print(y, "World")
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 left justified the String 'Hello' using 'ljust( )' Function.
That shifts the String Hello towards the left filling the last 5 locations with space (i.e. ' ').
And if you see the print statement,
There is a gap of 5 spaces between 'Hello' and 'World'.