How Can We Help?
In this tutorial, we will explain how to use Python string rjust() method with basic syntax by example for better understanding.
The String rjust() method or String right Justify Method returns an original string justified on the right-hand side then complete with a filled specific character or space to match specific width.
Syntax:
<string_value/string_variable>.rjust(width, fill character)
Input Parameters:
- string_value / String variable : The string that we want to justify it with a specific length.
- width : The length of right justified character.
- fill character: The character you want to justify the string. It’s an optional argument; If it’s not specified, Python will use space instead.
Python String rjust with space
We Can justify a string value on the right-hand side and complete it with a spaces to match a particular python length by using the rjust function without passing the second parameter (fill character).
Example (1)
# The String Value That We Want To Perform right justify Against
str1='HelloPython'
#Print String After right justify with spaces
print('Left adjust: ',str1.rjust(25))
Output of example (1)
Right adjust: HelloPython
In the above example, we have right justified the value of string variable ‘str1’ by ‘ ‘ up to width of string is 25 so output shows the result as shown above.
Python String rjust with character
We Can justify a string value on the right-hand side and complete it with a particular character to match a specific python length using the rjust function by specifying the second parameter (fill character).
Example (2)
# The String Value That We Want To Perform right justify Against
str1='HelloPython'
#Print String After right justify with $ Character
print('Left adjust: ',str1.rjust(25,'$'))
Output of example (2)
$$$$$$$$$$$$$$HelloPython
In the above example, we have right justified the value of string variable ‘str1’ by ‘#’ upto width of string is 20 so output shows the result as shown above.
In this tutorial, you have learned the string rjust() method in Python. What is it? And how to justify a string on the right-hand side.
Hopefully, it was clear and concise.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python String Formatting Methods:
- title() : It returns the “title cased” version of string means all the words start with uppercase and the remaining are lowercase.
- capitalize() : It capitalizes the first character of the String.
- center() : It returns a space-padded string with the original string centered to a total of width columns.
- swapcase() : It inverts the case of all the characters in a string.
- upper() : It converts all the characters of a string to uppercase.
- lower() : It converts all the characters of a string to lowercase.
- strip() : It removes particular characters of a string from left and left side.
- rstrip() : It removes particular character of a string from right side.
- lstrip() : It removes particular character of a string from left side.
- ljust() : It returns an original string with a filled specific character or space at the left side of the string value to justify the string’s new length (width).
- zfill() : It returns an original string value with filled zero at the left side of the string value to justify the string’s new length (width).
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)
Python Tutorials List:
- Python tutorials list : access all python tutorials.