How to get a space-padded string with the original string right-justified in Python?


A string is a collection of characters that can represent a single word or a complete phrase. We can declare a string or any other datatype in python without the datatype specifier. Therefore, it is easy to use strings in Python compared to Java.

A string in Python is represented by the class named String. This class provides several functions and methods using which you can perform various operations on strings.

In this article, we are going to focus on how to get a space padded string with the original string right justified in python.

Using the rjust() method

The python rjust() method is used to align the string to the right with the specified number of positions. Following is the syntax of this function −

string.rjust(length, character)

where,

  • length is the number of spaces you want to fill (this number includes the length of the string; if the number is less than the length of the string, no change will be made).

  • character is an optional parameter that fills the spaces with the specified character (if no character is specified, spaces will be padded).

Example 1

In the program given below, we are using rjust method for padding the given string with spaces.

str1 = "Welcome to Tutorialspoint" str2 = str1.rjust(30) print("Padding the string: ",str1) print(str2)

Output

The output of the above program is,

('Padding the string: ', 'Welcome to Tutorialspoint')
     Welcome to Tutorialspoint

Example 2

In the example given below, we are using rjust method for padding the given string with the symbol “$

str1 = "Welcome to Tutorialspoint" str2 = str1.rjust(30,'$') print("Padding the string ",str1) print(str2)

Output

The output of the above given example is,

('Padding the string ', 'Welcome to Tutorialspoint')
$$$$$Welcome to Tutorialspoint

Example 3

In the example given below, we are using rjust and padding the left side of the string with the symbol “0

str1 = "this is string example....wow!!!"; print(str1.rjust(50, '0'))

Output

The output of the above given program is,

000000000000000000this is string example....wow!!!

Using the format() method

The python format() method is the next option. For filling up gaps and padding the string, we may use the string format technique. This method is often used with a print statement.

We'll use a colon (:) to indicate the amount of spaces in a curly brace that need to be filled in order to provide the proper padding. To add left padding we should also add the > symbol.

Example

In the example given below, we are taking a string as input and we are using the format method and padding the left side of the string.

str1 = "Welcome to Tutorialspoint" str2 = ('{:>35}'.format(str1)) print("Left Padding of the string ",str1) print(str2)

Output

The output of the above program is,

('Left Padding of the string ', 'Welcome to Tutorialspoint')
          Welcome to Tutorialspoint

Updated on: 19-Oct-2022

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements