Python String ljust() and rjust() functions

Ljust() And Rjust() Thumbnail

Python ljust() and rjust() functions are used to align a string, they can align a string left or right by inserting some specified character at the opposite site. For example, the ljust() function inserts the specified character to the right so the string aligns at the left. The character is whitespace by default. In this tutorial, we will learn about both these functions performing string operations.

Also Read: How to Display Numbers with Leading Zeros in Python

Python String ljust() function

The Python string ljust() function accepts characters for padding in an input string by adding it to the right of the input string. It also takes a length as an additional argument which decides the length of the string, if the length is less then it will keep adding the specified characters multiple times to get the required length.

Syntax:

string.ljust(length,fillChar)

Parameters:

  • length: The length specifies the width of expansion of the input string
  • fillChar: This is an optional parameter that determines the character insert at the right side of the string

Return Value:

The ljust() function returns a new string where the given fillChar is substituted to the right of the original string.

Examples of ljust() function in Python

Let’s now look at some examples of the ljust() function to demonstrate how it works.

Example 1:

input_string = 'Python'
size = 9
fill_char = '@'

print(input_string.ljust(size, fill_char)) 

Output:

Python@@@

Example 2:

inp_str = "Engineering Discipline"

print ("Input string: \n",inp_str) 

print ("Left justified string: \n") 
print (inp_str.ljust(30, '*')) 

Output:

Input string: 
Engineering Discipline
Left justified string: 
Engineering Discipline********

NumPy string operations using ljust() function

This function can also be used to perform string operations in NumPy.

Syntax:

np.char.ljust(inputArray, length, fillChar) 

Parameters:

  • inputArray: the inputArray is a NumPy array
  • length: the length specifies the width of expansion of the input string
  • fillChar: this is an optional parameter that determines the character insert at the right side of the string

Example:

import numpy as np

input_arr = np.array(['Safa', 'Aman']) 
print ("Input array : ", input_arr) 

len1 = 10

output_arr = np.char.ljust(input_arr, len1, fillchar ='%') 
print ("Output array: ", output_arr) 

Output:

Input array :  ['Safa' 'Aman']
Output array:  ['Safa%%%%%%' 'Aman%%%%%%']

Python String rjust() function

The Python string rjust() function accepts characters for padding in an input string by adding it to the left of the input string. It also takes a length as an additional argument which decides the length of the string, if the length is less then it will keep adding the specified characters multiple times to get the required length.

Syntax:

string.rjust(length,fillChar)

Parameters:

  • length: the length specifies the width of expansion of the input string
  • fillChar: this is an optional parameter that determines the character insert at the left side of the string

Return Value:

The rjust() function returns a new string where the given fillChar is substituted to the left of the original string.

Examples of rjust() function in Python

Let’s now look at some examples of the rjust() function to demonstrate how it works.

Example 1:

input_string = 'Mary'
size = 7
fill_char = '@'

print(input_string.rjust(size, fill_char)) 

Output:

@@@Mary

Example 2:

inp_str = "Engineering Discipline"

print ("Input string: \n",inp_str) 

print ("Right justified string: \n") 
print (inp_str.rjust(30, '*')) 

Output:

Input string: 
 Engineering Discipline
Right justified string: 

********Engineering Discipline

NumPy string operations using rjust() function

This function can also be used to perform string operations in NumPy.

Syntax:

np.char.rjust(inputArray, length, fillChar) 

Parameters:

  • inputArray: the inputArray is a NumPy array
  • length: the length specifies the width of expansion of the input string
  • fillChar: this is an optional parameter that determines the character insert at the left side of the string

Example:

import numpy as np


input_arr = np.array(['Safa', 'Aman']) 
print ("Input array : ", input_arr) 


len1 = 10


output_arr = np.char.rjust(input_arr, len1, fillchar ='%') 
print ("Output array: ", output_arr) 

Output:

Input array :  ['Safa' 'Aman']
Output array:  ['%%%%%%Safa' '%%%%%%Aman']

Conclusion

In this tutorial, we have learned to align a string in Python using two methods ljust() and rjust() which align a string left and right respectively.

In this tutorial, we have covered two methods of string operation, but there are many more built-in functions available in Python for almost every operation you can perform on a string, we have a separate tutorial on that, click here to read.

Reference

String operations — NumPy v1.24 Manual