Menu Close

Python String rjust() Method

Python string rjust

In this article, you will learn about the Python string rjust() method along with examples. Python string rjust method is used to right-align the string and fill spaces to a 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 swapcase() method to convert lower case to uppercase and vice-versa.

Python string rjust() method

Python rjust() string method right justify the string and fill the remaining spaces with character.
This method returns a new string justified right and filled with character.

Syntax

The syntax of rjust function in Python is:-

string.rjust(length, character)

Parameter

The rjust 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.

Return Value

rjust function in Python returns a new string containing a specified character and actual string.

Python string rjust example

To understand python string rjust method, here we will take some examples.

Example 1:

#use string rjust method with first parameter.
a = 'Programming'
result = a.rjust(20)
print(result)

Output

         Programming

Note: In the result, there are actually 9 whitespaces to the left of the word Programming.

Example 2:

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

Output

---------Programming

Example 3

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

Output

>>>>>>>>>>>>>Programming Funda

Conclusion

In this tutorial, you have learned the Python string rjust method along with various examples.
This is the best string method to right-align the string and fill the remaining spaces with a specified character.

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 swapcase() Method
Python String rstrip() Method

Related Posts