×

Python String rjust() method

Python String rjust() method

The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns the original string if the argument ‘width’ is less than the length of the string.

Syntax

string.rjust(width[, fillchar])

Parameter

width: This parameter represents the width of the given string. If width is less than or equal to the length of the string, original string is returned.

fillchar: This parameter represents the character to fill the remaining space of the width

Return

This method returns a right-justified string of a given minimum width if the specified width is more than the length of the string else it returns the original string. 

Example 1

 # Python programming explaining
 # the string.rjust() method
 # initializing the string
 string = "Python"
 # right aligning the string, using whitespaces(by default it is whitespaces)
 val = string.rjust(20)
 # printing the value
 print(val, "is an easy language to learn.") 

Output

              Python is an easy language to learn.

Example 2

 # Python program explaining
 # the string.rjust() method
 # initializing the string
 string = "Hello World"
 # Printing the actual string 
 print ("The actual string:", string, "\n") 
 # Printing the right aligned  
 # string with "!" padding  
 print ("The right aligned string:", string.rjust(40, '!')) 

Output

The actual string: Hello World 
The left aligned string: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!Hello World

Example 3

 # Python program explaining
 # the string.rjust() method
 # initializing the string
 string = "Morning"
 # Printing the actual string 
 print ("The actual string:", string, "\n") 
 # Printing the right aligned  
 # string with "*" padding  
 print ("The right aligned string with width 40:", string.ljust(40, '*')) 
 # will return the same string the width is less than the length of the string
 print("The right aligned string with width 2:", string.ljust(2, '*')) 

Output

 The actual string: Morning 
 The right aligned string with width 40: Morning*********************************
 The right aligned string with width 2: Morning 

Related Topics

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Python Unit Test Cheat String

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

Spark and Python for big data with pyspark github

Pyspark: Apache Spark is a computational engine that handles large data sets using parallel and batch processing. Apache Spark is an open-source, distributed computing framework and collection of libraries for real-time,...

3 minutes read.

Find Last Occurrence of Substring using Python

Introduction When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to...

3 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Python Breakpoint

Introduction In Python 3.7, a brand-new created function called breakpoint() was added. Due to the close relationship between both the executable and the code of a debugging component, debugging Python programming...

4 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python List insert() method

Python List insert() method The list.insert () method in Python inserts an item at the specified position. Syntax list.insert(i, x) Parameter i: This parameter represents a number specifying the position to insert the given value. x: This parameter signifies...

1 minute read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Calculator Program in Python

Simple Calculator Program in Python This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division. Source Code The following is the source...

2 minutes read.

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.