In this section, we will learn what String ljust() method is and how to use it in Python.
What is String ljust() Method in Python?
The Python string ljust() method is used to align the right side of a string value.
Basically, using this method we can increase the length of a string value and add new white spaces on the right side of that string so that it becomes align with other content.
Python String ljust() Method Syntax:
string.ljust(length,character)
String ljust() Method Parameter
The method takes two arguments:
- The first argument is an integer number that defines a length. This value declares the final length of the new string that will return from this method. Also this value declares how far the right side of the target string should be indented! For example, if the string value that this method is invoked upon has the length of 5, then setting the first argument to a value like 15, will cause to add 10 white spaces to the right side of that string.
- The second argument is a value (usually just a character) that we want to fill out spaces of the new string. By default, this value is set to white-space and that means the length that left out should be filled with white spaces! But we can use other characters if needed for this purpose.
String ljust() Method Return Value
The return value of this method is a new string value with its left side aligned according to the values we set for the method.
Example: using python string ljust() method
s1 = "My name is John Doe" print(s1.ljust(50, "*"))
Output:
My name is John Doe*******************************