Python - String Method - ljust() Tutorial
This method returns a left-justified(left-aligned) string of given minimum width, using a specified character (space as default) as the fill character.
Syntax-
String.ljust(length, fillchar)
#length - It is the total length of the string that needs to be returned
after filling the specified characters on the right end(ending) of the actual string.
#fillchar - (Optional) It is the specified character that needs to be filled
on the right end(ending) of the actual string. By default it is space
Example-
str1 = "Hello Python"
print(str1.ljust(30))
print(str1.ljust(30,'*'))
print(str1.ljust(2))
Output-
Hello Python
Hello Python******************
Hello Python
In the above program,
Line 2- str1 is aligned at the left and the remaining is aligned with spaces on the right, with the total length of the new string as 30
Line 3- str1 is aligned at the left and the remaining is aligned with specified character "*" on the right, with the total length of the new string as 30
Line 4- On providing length less than the original string. then it will return the original string.