ljust() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Python offers many functions to align the string or add user-specified padding instead of blank space in the string. These functions are ljust, rjust, and center, which are used to align the string differently.

Syntax for ljust() in Python

Parameters for ljust() in Python

  1. width: (Required), the width of the string till it is expanded. The original string will be returned if it is less than or equal to the specified string's length.
  2. fillchar: (Optional), A character to fill the remaining space in the left-justified string if the width is greater than its length. If the character is not specified, a blank space will be added right to the string.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Value for ljust() in Python

It returns a new string of a specific length after inserting a specified character, i.e., fillchar on the right side of the old string. If fillchar is specified, the remaining space is filled with the specified character.

Example for ljust() in Python

Output:

In the above example, you can see that text.ljust(8)

creates a new string of length 10 and makes the given string left-justified. Since we have not defined the fillchar, the default value space is added.

Similarly, text.ljust (8, "-")

gives us output as Scaler--.

Here ljust function appends two - as a fillchar to make the string of the given length. If the given width is the same or less than the length of the original string, the function will do nothing. It will just return the original string, as you can see above in the third example.

What is ljust() in Python?

ljust in python is a function that returns a left-justified string with a length equal to the specified width. It fills the remaining string space with blank spaces if the fillchar argument is not passed.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

How to Use ljust() in Python?

ljust is a method in Python that is used to the left specified the given string. It takes two parameters as an argument, i.e., width and fillchar.

The width specifies the maximum length to which the given string will be expanded, where fillchar specifies which character should be used to fill in the blank spaces in the string after it has been left-justified.

It returns a new string of a specific length after inserting a specified character, i.e., fillchar on the right side of the old string.

Note: The fillchar character must be a single character. If we give fillchar more than one character, it will throw a TypeError.

More Examples

Example 1

Output:

In the above example, you can see that text.ljust(10)

creates a new string of length 10 and makes the given string left-justified. Since we have not defined the fillchar, the default value space is added.

Similarly, text.ljust (10, "$")

gives us output as Scaler$$.

Here ljust() function appends two $ as a fillchar to make the string of the given length. If the given width is the same or less than the length of the original string, the function will do nothing. It will just return the original string, as you can see above in the third example.

Example 2

Output:

The fillchar character must be a single character. If we give fillchar more than one character, it will throw a TypeError, as shown above.

Conclusion

  • ljust() in python is a function that returns a left-justified string with a length equal to the specified width.
  • It returns a new string of a given length after substituting a given character on the original string's right side.
  • The fillchar character must be a single character. If we give fillchar more than one character, it will throw a TypeError.

See Also:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more