Methods and Functions

Python String ljust()

String alignment is commonly utilized in a wide range of everyday applications. Python’s language includes various functions that aid in string alignment. It also allows you to provide user-specified padding instead of blank space. The Python string API has two utility functions for generating a new string of a defined length from a source string with right and left justification. One of them being the Python ljust() function that is a common string handling method. We will learn more about Python ljust() and its programming applications in the article below.

Python ljust() function

Definition

  • Python ljust() is a built-in function that returns a left-justified string according to the width specified and fills the remaining spaces with blank spaces if the character argument is not passed.
  • The Python ljust() string method returns a left-padded string of a given minimum width while using a specific character to fill the additional position of the string.

Python ljust() Syntax

The syntax followed by Python ljust() function is as below:

                    

string.ljust(width, fillchar)

ljust() Parameters

Python ljust() accepts 2 parameters:

  • width – length of the string to be expanded. If the width is less than or equal to the total length of the string, then the original string is returned.
  • fillchar (Optional) – character to be filled in the remaining spaces of the width. The default value is blank spaces.

Note – Only 1 specific character is to be mentioned in the fillchar argument. Else Python interpreter throws a TypeError exception.

Return value from ljust()

It returns a left-padded string with fillchar character added in place of spaces if the fillchar argument is specified.

Example 1: Left justify string of minimum width

                    

# Python program to illustrate ljust()
txt = 'Python'
print('Original string:', txt)

# left-justified string
r = txt.ljust(10)
print('Left-justified string:', r)

Output

                    

Original string: Python
Left-justified string: Python

Explanation

In the above example, considering the first print statement, txt.ljust(10) generates a new string of length 10 and aligns ‘Python’ to the left. Because a fillchar is not specified, the default value space is utilized. So, the result is ‘Python’ with 4 spaces on the right side.

Example 2: Left justify string and fill the remaining spaces

The input string is first aligned to the left, and then the remaining spaces on the right are filled with the provided character using the fillchar parameter.

Example

                    

# Python program to illustrate ljust()
txt = 'Programming'
print('Original string:', txt)

# left-justified string with fillchar
r = txt.ljust(20, '*')
print('Left-justified string:', r)

txt = 'Hello'
print('Original string:', txt)

# left-justified string with fillchar
r = txt.ljust(10, '$')
print('Left-justified string:', r)

Output

                    

Original string: Programming
Left-justified string: Programming*********

Original string: Hello
Left-justified string: Hello$$$$$

Example 3: TypeError Exception

The fillchar parameter must only contain one character. If more than one character is supplied as fillchar, it will throw a TypeError, as illustrated below.

Example

                    

txt = 'Swimming'

# left-justified string with multiple fillchar characters
r = txt.ljust(20, '->*')
print('Left-justified string:', r)

Output

                    

Traceback (most recent call last):
File "", line 4, in 
TypeError: The fill character must be exactly one character long

Frequently Asked Questions

Q1. What does ljust mean in Python?

Python ljust() is a built-in function that returns a left-justified string according to the width specified and fills the remaining spaces with blank spaces if the character argument is not passed.

The ljust() function returns a new string of the specified length with the source string left-justified. The padding character can be specified; the default is whitespace. If the requested length is less than the length of the source string, the source string is returned.

Q2. How do you use the ljust() in Python?

The Python ljust() string method returns a left-padded string of a given minimum width while using a specific character to fill the additional position of the string. The syntax followed by the Python ljust() function is as below:

                    

string.ljust(width, fillchar)

Example

                    

txt = 'Raining'

# left-justified string with fillchar
r = txt.ljust(15)
print('Left-justified string:', r)

# left-justified string with fillchar
r = txt.ljust(15, '-')
print('Left-justified string:', r)

Output

                    

Left-justified string: Raining        
Left-justified string: Raining--------

Q3. What is the difference between rjust and ljust in Python?

Python rjust() and ljust() function are a part of Python string manipulation method. They are somewhat similar but the difference between them is:

  • Python rjust() – If the ‘fillchr‘ parameter is not given, this function right aligns the string according to the width specified and fills the leftover space of the line with blank space.
  • Python ljust() – If the ‘’fillchr‘ parameter is not given, this function left aligns the string according to the width specified and fills the remaining space of the line with blank space.

Example

                    

txt = 'Welcome'

# right-justified string with fillchar
r = txt.rjust(14, '*')
print('Right-justified string:', r)

# left-justified string with fillchar
r = txt.ljust(14, '*')
print('Left-justified string:', r)

Output

                    

Right-justified string: *******Welcome
Left-justified string: Welcome*******

Q4. What does zfill mean in Python?

The zfill() function is a Python built-in function that pads 0s on the left side of a string to fill the specified width and returns a duplicate of the string. It generates a padded string by appending zeros. The Python string zfill() function effectively adds zeros (0) at the beginning of the string until it reaches the required length. The important thing to remember here is that if the length parameter value is less than the length of the string, no filling is performed.

Syntax

                    

string.zfill(width)

Example

                    

txt = 'Python'
r = txt.zfill(10)
print(r)

txt = '+World'
r = txt.zfill(10)
print(r)

Output

                    

0000Python
+0000World

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.