Sunday, September 7, 2025
HomeLanguagesString rjust() and ljust() in python()

String rjust() and ljust() in python()

1. String rjust() The string rjust() method returns a new string of given length after substituting a given character in left side of original string.

Syntax:

string.rjust(length, fillchar)

Parameters:

length: length of the modified string. If length is less than or equal to the length of the original string then original string is returned.
fillchar: (optional) characters which needs to be padded. If it’s not provided, space is taken as a default argument.

Returns:

Returns a new string of given length after substituting a given character in left side of original string.

Example




# Python program to demonstrate working of 
# rjust()
string = 'Lazyroar'
length = 8
  
# If no fill character is provided, space
# is used as fill character
print(string.rjust(length))


Output:

   Lazyroar

Example




# example string
string = 'Lazyroar'
length = 8
fillchar = '*'
  
print(string.rjust(length, fillchar))


Output:

***Lazyroar  

 

2. String ljust()
The string ljust() method returns a new string of given length after substituting a given character in right side of original string.

Syntax:

string.ljust(length, fillchar)

Parameters:

length: length of the modified string. If length is less than or equal to the length of the original string then original string is returned.
fillchar: (optional) characters which needs to be padded. If it’s not provided, space is taken as a default argument.

Returns:

Returns a new string of given length after substituting a given character in right side of original string.

Example 1




# example string
string = 'Lazyroar'
length = 8
  
# If no fill character is provided, space
# is used as fill character.
print(string.ljust(length))


Output: (Three spaces are printed after Lazyroar)

Lazyroar   

Example 2




# example string
string = 'Lazyroar'
length = 8
fillchar = '*'
  
# print left justified string
print(string.ljust(length, fillchar))


Output:

Lazyroar***  
RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6641 POSTS0 COMMENTS
Nicole Veronica
11807 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11870 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS