The ljust function in Python is utilized to left-align a string while filling the remaining gaps with specified fill characters. This function generates a new string that is left-justified and supplemented with the designated fill characters.
Syntax of Python String Ijust Method
It has the following syntax:
Example
ljust(width[, fillchar])
Parameters
- width: width of the given string.
- fillchar: characters to fill the remaining space in the string. It is optional .
Return
It returns a left justified string.
Different Examples for Python String Ijust Method
Let us explore various examples of the ljust method to gain a clearer understanding of its capabilities.
Example 1
Let's consider an example to illustrate the functionality of the ljust method in Python.
Example
# Python ljust() method example
# Variable declaration
str = 'Example'
# Calling function
str = str.ljust(20)
# Displaying result
print(str)
Output:
| J | a | v | a | t | p | o | i | n | t |
|---|
Example 2
To illustrate the functionality of the ljust method in Python, we will use a specific example.
Example
# Python ljust() method example
# Variable declaration
str = 'Example'
# Calling function
str = str.ljust(15,"$")
# Displaying result
print(str)
Output:
| J | a | v | a | t | p | o | i | n | t | $ | $ | $ | $ | $ |
|---|