Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
rjust in Python
2.1.
Syntax
2.2.
Return Value
3.
Example 1
3.1.
Output
4.
Example 2
4.1.
Output
5.
Example 3
5.1.
Output
6.
Frequently Asked Questions
6.1.
Rjust in Python is short for?
6.2.
Is Python's string mutable?
6.3.
How do you create a string in Python?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Exploring the Rjust in Python

Author Shiva
0 upvote

Introduction 

Hello, readers. In this article, we will learn about rjust in Python. rjust is a method that is defined under the String class. Strings are sequences of characters that are represented by single or double quotes. Strings in Python are collections of bytes that represent Unicode characters. 

introductory image

Here’s an interesting thing about Python. Did you know that there is no such thing as a character data type in Python? A single character in Python is just a string of length 1! To access the string's elements, use square brackets.

Also see, Intersection in Python, and Convert String to List Python

rjust in Python

rjust in Python is a method defined under the String class. Rjust in Python right-justifies the string. This means that the method will prepend the given string with the passed “character”. The length of the resultant string would be equal to the length passed in the argument. In case the length of the string is already greater than the passed length limit, it will not prepend.

But, Why do we use rjust in Python? 
Just like the rjust() method, there is ljust() method too. These methods are just formatter to justify the content.

Let’s understand all these with examples. 

Syntax

The syntax of rjust in Python: 

string_name.rjust(length_limit, fill_char)

 

  • length_limit: It is used to limit the length of the resultant string.
     
  • fill_char: You can pass any “character” here. The method will prepend the string with that. Note that fill_char is an optional parameter. If not passed, the default value would be ‘ ‘(space).

Return Value

Rjust in Python returns the modified string, i.e., the data type of the returned value is String. 

Example 1

# demo string
demo = 'example'
len_limit = 10


# printing right-justified string
print(demo.rjust(len_limit))

Output

output 1

Example 2

# demo string
demo = 'example'
len_limit = 5


# printing right-justified string
print(demo.rjust(len_limit))

Output

output 2

There is no change in the output because the length of the original string exceeds the defined string length limit. It is important to note that returned string length is more than the defined length limit. 

Example 3

# demo string
demo = 'example'
len_limit = 10
# printing right-justified string
print(demo.rjust(len_limit, "*"))

Output

output 3

You can practice by yourself with the help of online python compiler.

Frequently Asked Questions

Rjust in Python is short for?

Rjust is short for right justified. The method prepends the given string with the passed “character.”

Is Python's string mutable?

Python strings are "immutable." They cannot be modified after they have been formed (Java strings also use this immutable style). 

How do you create a string in Python?

Put the string of characters inside single, double, or even triple quotes to create a string, and then you can assign it to a variable.

Conclusion

In this article, we learned about rjust in Python, its syntax, return value, and some examples. 

If you want to explore more, here are some related articles - 


Check out this problem - Multiply Strings

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninjas!

Live masterclass