Menu Close

Python String ljust() Method

python string ljust

In this article, you will learn about the Python string ljust() method along with examples. Python string ljust method is used to left-align the string and fill spaces to specific character.

This method will only work with the string because it is a string method. In the previous tutorial, we have seen, Python string split() method to split the string.

Python string ljust() method left justify the string and fill the remaining spaces with characters. This method returns a new left string justified with filled characters ( Space is the default ).

Syntax

The syntax of ljust function in Python is:-

string.ljust(length, characters)

Parameter

The ljust function in python accepts two arguments.

  • length:- Required, The length of the returned string.
  • character:- A character to fill the missing space. Default is space.

Python string ljust example

To understand ljust function in Python, here we will take some examples.

Example 1:

#use string ljust method without second parameter.
a = 'Programming'
result = a.ljust(20)
print(result)

Output will be:- ‘Programming

Example 2:

#use string ljust method with first and second parameter.
a = ['Programming']
result = str1.ljust(20, '-')
print(result)

Output will be:- ‘Programming———

Example 3:

#use string ljust method with first and second parameter.
a = 'Programming Funda'
result = a.ljust(40, '>')
print(result)

Output will be:- ‘Programming Funda>>>>>>>>>>>>>>>>>>>>>>>

Conclusion

In this tutorial, you have learned the Python string ljust method along with various examples. This is the best string method to left-align the string and fill the remaining spaces with specified characters.

I hope this tutorial will help you. if you like this article, please share it with your friends who want to learn Python programming.

Other string methods


For More Information:- Click Here

Python String rfind() Method
Python String startswith() Method

Related Posts