Python String rjust Method

The rjust method in Python is utilized to align a string to the right, filling any unused space with specified fill characters. This method generates and returns a new string that is right-justified and includes the designated fill characters in the remaining areas.

Syntax of Python String rjust Method

It has the following syntax:

Example

rjust(width[, fillchar])

Parameters

  • width: width of the given string.
  • fillchar: characters to fill the remaining space in the string. It is optional.
  • Return

It returns a right justified string.

Different Examples for Python String rjust Method

Let’s examine a few instances of the rjust method to grasp its functionalities more effectively.

Example 1

In this section, we will illustrate the use of the Python String rjust method through a specific example.

Example

# Python rjust() method example

# Variable declaration

str = 'Example'

# Calling function

str = str.rjust(20)

# Displaying result

print(str)

Output:

j a v a p o i n t

Example 2

In this section, we will illustrate the usage of the Python String rjust Method through a practical example.

Example

# Python rjust() method example

# Variable declaration

str = 'Example'

# Calling function

str = str.rjust(15,"$")

# Displaying result

print(str)

Output:

$ $ $ $ $ j a v a p o i n t

Input Required

This code uses input(). Please provide values below: