In the previous article, we have discussed Python String rindex() Method Examples
rjust() Method in Python:
The rjust() method will right-align the string, using a specified character as the fill character (space is the default).
Syntax:
string.rjust(Length, character)
Parameters
Length: This is required. It is the length of the string returned.
character: This is optional. Fills the character in the missing space( left of the string). The default value is ” ” (space).
Examples:
Example1:
Input:
Given first string = "python" Given length = 15 Given character = '-'
Output:
The above given first string is : python The given first string after applying rjust() function: ---------python
Example2:
Input:
Given second string = "GOODMORNING" Given length = 17 Given character = '#'
Output:
The above given second string is : GOODMORNING The given second string after applying rjust() function: ######GOODMORNING
String rjust() Method Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Give the length as static input and store it in another variable.
- Give the character as static input and store it in another variable.
- Apply rjust() method to the given string for the given length and the character which will right-align the string, using a specified character as the fill character (space is the default).
- Print the above-given string.
- Print the above-given string after applying the rjust() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable.
gvn_fststr = "python"
# Give the length as static input and store it in another variable.
gvn_lengt = 15
# Give the character as static input and store it in another variable.
gvn_chactr = '-'
# Apply rjust() method to the given string for the given length and the
# character which will right align the string, using a specified character
# as the fill character (space is the default).
# Store it in another variable.
rslt_str1 = gvn_fststr.rjust(gvn_lengt, gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying rjust() function.
print("The given first string after applying rjust() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "GOODMORNING"
gvn_lengt2 = 17
gvn_chactr2 = '#'
rslt_str2 = gvn_scndstr.rjust(gvn_lengt2, gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rjust() function:", rslt_str2)
Output:
The above given first string is : python The given first string after applying rjust() function: ---------python The above given second string is : GOODMORNING The given second string after applying rjust() function: ######GOODMORNING
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Give the length as user input using the int(input()) function and store it in another variable.
- Give the character as user input using the input() function and store it in another variable.
- Apply rjust() method to the given string for the given length and the character which will right-align the string, using a specified character as the fill character (space is the default).
- Print the above-given string.
- Print the above-given string after applying the rjust() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some random string = ")
# Give the length as user input using the int(input()) function and store it in another variable.
gvn_lengt = int(input("Enter some random number = "))
# Give the character as user input using the input() function and store it in another variable.
gvn_chactr = input("Enter some random charcter = ")
# Apply rjust() method to the given string for the given length and the
# character which will right align the string, using a specified character
# as the fill character (space is the default).
# Store it in another variable.
rslt_str1 = gvn_fststr.rjust(gvn_lengt, gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying rjust() function.
print("The given first string after applying rjust() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some random string = ")
gvn_lengt2 = int(input("Enter some random number = "))
gvn_chactr2 = input("Enter some random charcter = ")
rslt_str2 = gvn_scndstr.rjust(gvn_lengt2, gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rjust() function:", rslt_str2)Output:
Enter some random string = hello Enter some random number = 10 Enter some random charcter = % The above given first string is : hello The given first string after applying rjust() function: %%%%%hello Enter some random string = btechgeeks Enter some random number = 20 Enter some random charcter = $ The above given second string is : btechgeeks The given second string after applying rjust() function: $$$$$$$$$$btechgeeks
Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.
- Python String rpartition() Method Examples
- Python String rsplit() Method Examples
- Python String rstrip() Method Examples
- Python String strip() Method Examples