In this tutorial, we’ll learn what Python string rjust() method is and how to properly use it. We’ll go through multiple Python code examples to understand how rjust() method works in Python.
Introduction: Python String rjust() Method
This method is used to return a right justified Python string having a specified minimum width.
Syntax of rjust() Method
string.rjust(width,char(optional))
- Python string rjust() method take two arguments.
- First one specifies the width of the new returned string. If the width is less than or equal to the original string then only the string will be returned.
- Second argument is optional. Its used to fill the remaining string with a character specified in it. Whitespace character will be used in case no character is passed to it.
Example 1: rjust() Method Applied on a Python String
val='Python Programming' print(val.rjust(20,'*')) // whitespace applied as no character is passed print('Python'.rjust(23)) print('Python'.rjust(12,'*')) print('Python'.rjust(30,'*')) print('Python'.rjust(19,'-'))
Output
**Python Programming Python ******Python ************************Python -------------Python
We can see that by providing a width of equal or less than the actual length of string will result in just the original string returned by Python string rjust method. Also, whitespaces will be applied by default if no other character is passed.
Example 2: Python String rjust() Method Return Value
val= 'Python Programming' returnedValue= val.rjust( 24, '+' ) print( returnedValue ) print( val ) // original string
Output
++++++Python Programming Python Programming // original string not modified
As we can see that Python string rjust method does not modify the original string. It returns a new string with specified modifications.
Click here if you are interested in learning Python string ljust method in detail.
Conclusion
To conclude this tutorial, I hope you now have an in-depth practical knowledge of how to properly use Python string rjust method. I’d love to receive your valuable feedback on this post. Thank you for reading it.