Python String rjust() Method Example
Python3
string = 'geeks' length = 8 print (string.rjust(length)) |
Output:
geeks
Example 1: Python String rjust() Method with ‘fillchar’ argument provided
Here we have defined a Python string and right-adjusted it to 8 characters with fillchar as ‘*‘.
Python3
# example string string = 'geeks' length = 8 fillchar = '*' print (string.rjust(length, fillchar)) |
Output:
***geeks
Example 2: Practical Example using rjust() Method
Here, we have used String rjust() method to create the half-diamond pattern.
Python3
string = "awesome" max_width = len (string) fill_char = "_" # create the list of strings string_lst = [string[: len (string) - index] for index in range ( len (string))] for item in sorted (string_lst, key = len ): print (item.rjust(max_width, fill_char)) for item in sorted (string_lst[ 1 :], key = len , reverse = True ): print (item.rjust(max_width, fill_char)) |
Output:
______a _____aw ____awe ___awes __aweso _awesom awesome _awesom __aweso ___awes ____awe _____aw ______a
Python String rjust() Method
Python String rjust() method returns a new string of a given length after substituting a given character on the left side of the original string.