Python String ljust() Method



The Python String ljust() method is used to left align the string with the width specified. If the specified width is more than the length of the string, then the remaining part of the string is filled with fillchar.

The default fillchar is a blank space. The original string is retrieved if the width is less than or equal to the given string length.

Note: Only one specific character can be mentioned to fill the remaining part of the string with fillchar.

Syntax

Following is the syntax of Python String ljust() method:

str.ljust(width[, fillchar])

Parameters

  • width − This is string length in total after padding.

  • fillchar − This is the filler character; it’s default is a space (Optional).

Return Value

This method returns a left justified string with the fillchar specified as an argument in place of blank spaces. The original string is returned if width is less than string length.

Example

In the following example the created string "this is string example....wow!!!" is aligned to the left. Then the remaining blank spaces on the right are filled with the specified character "0" as the fillchar argument using Python String ljust() method. The result is then retrieved:

# Initializing the string
str = "this is string example....wow!!!";
print (str.ljust(50, '0'))

When we run above program, it produces following result:

this is string example....wow!!!000000000000000000

Example

Following is an example where a new string of length 89 is generated and aligns the created string ‘Programming’ to the left. Since the fillchar is not provided, the default value of blank space is used. Therefore, the result ‘Programming’ with 78 blank spaces on its right is retrieved:

text = 'Programming'
# left-aligning the string
x = text.ljust(89)
print('The string after aligning is:', x)

While executing the above code we get the following output:

The string after aligning is: Programming                                                                              

Example

In the example given below we are creating a dictionary with 3 key-value pairs. Then we are trying print the value pairs seperated by ":" we are using ljust() method to do so.

# providing the dictionary
dictionary = {'Name':'Sachin', 'Sports':'Cricket', 'Age':49}
# iterating on each item of the dictionary
for keys, value in dictionary.items():
   print(str(keys).ljust(6, ' '),":", str(value))

Output of the above code is as follows:

Name   : Sachin
Sports : Cricket
Age    : 49     

Example

Following is an example to show that if more than one character is passed as the fillchar argument, it will throw an error because the fillchar argument should contain only one character:

text = 'Coding'
# providingh more than one fillchar character
x = text.ljust(67, '*#')
print('The new string is:', x) 

Following is the output of the above code:

Traceback (most recent call last):
   File "C:\Users\Lenovo\Desktop\untitled.py", line 3, in 
      x = text.ljust(67, '*#')
TypeError: The fill character must be exactly one character long
python_strings.htm
Advertisements