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.
Headings of Contents
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
- String endswith() method
- String casefold() method
- String center() method
- String capitalize() method
- String count() method
- String index() method
- String format() method
- String isalpha() method
- String isidentifier() method
- String islower() method
- String isupper() method
For More Information:- Click Here