String method rjust() in Python
The string method rjust() used to right align the string by specified length and character as the fill character. If character value not specified, the default value will be considered as space.
Syntax
 1 <string>.rjust(<length>, <character>)
string : A string value to right align using specified length and fill character
length : It specifies a string length after aligning to the right with filled character
character : A character to fill the leading space of the string, the default value will be space, optional
String method rjust()
 1 str1 = 'Hello Python'
 2 str2 = 'Hello world in python'
 3 
 4 r1 = str1.rjust(20) # default fill char is space
 5 r2 = str2.rjust(20) # default fill char is space
 6 
 7 print('A resulting string :', r1)
 8 print('A resulting string :', r2)
In the above example, a string variable is define with initial value. A rjust() method called without padded characters. If padded character not specified in the rjust() method, it filled with the white space while aligning. It returns an aligned string and assigned to the variable that will be printed on console.
Output
 1 A resulting string :         Hello Python
 2 A resulting string : Hello world in python

rjust() with pad character

rjust() with pad character
 1 str1 = 'Hello Python'
 2 str2 = 'Hello world in python'
 3 
 4 r1 = str1.rjust(20, '*')
 5 r2 = str2.rjust(20, '*')
 6 
 7 print('A resulting string :', r1)
 8 print('A resulting string :', r2)
In the above example, a rjust() method called by specifying a length and padding character. It creates a string with right padding with specified character star (*) and returns a resulting string.
Output
 1 A resulting string : ********Hello Python
 2 A resulting string : Hello world in python

rjust() with length less than source string

rjust() with length less than source string
 1 str1 = 'Hello Python'
 2 str2 = 'Hello world in python'
 3 
 4 r1 = str1.rjust(10, '*')
 5 r2 = str2.rjust(10, '*')
 6 
 7 print('A resulting string :', r1)
 8 print('A resulting string :', r2)
In the above example, a rjust() method called by specifying a length and padding character. As length of rjust() specified that is less than source string length which will returns an original string.
Output
 1 A resulting string : Hello Python
 2 A resulting string : Hello world in python
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us