Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python String ljust() and rjust() functions

Python String contains two built-in methods to provide the left and right justification.


1. Python String ljust() function

Python String ljust() function accepts the character for padding it to the input string. Then, it substitutes the character and does padding to the right of the input string.

Syntax:

String.ljust(length,fill_char)

Parameters:

  • length: The value of the length provided is basically used to justify the string of the given length.
  • fill_char: This is an optional parameter. These are the characters that needs to be padded around the string.

Value returned from the ljust() function:

The ljust() function returns a new string and substitutes the given fill_char to the right of the original string.

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 ljust() function

Syntax:

NumPy_array.char.ljust(input_array, length, fill_char) 

Parameters:

  • Input array
  • length: The value of the length provided is basically used to justify the string of the given length.
  • fill_char: This is an optional parameter. These are the characters which needs to be padded around 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%%%%%%']

2. Python String rjust() function

Python’s rjust() function accepts the character for padding it to the input string.

Then, it substitutes the character and does padding to the left of the input string.

Syntax:

String.rjust(length,fill_char)

Parameters:

  • length: The value of the length provided is basically used to justify the string of the given length.
  • fill_char: This is an optional parameter. These are the characters that needs to be padded around the string.

Value returned from the rjust() function:

The rjust() function returns a new string and substitutes the given fill_char to the left of the original string.

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 ("Left justified string: \n") 
print (inp_str.rjust(30, '*')) 

Output:

Input string: 
 Engineering Discipline
Left justified string: 

********Engineering Discipline

NumPy String rjust() function

Syntax:

NumPy_array.char.rjust(input_array, length, fill_char) 

Parameters:

  • Input array
  • length: The value of the length provided is basically used to justify the string of the given length.
  • fill_char: This is an optional parameter. These are the characters which needs to be padded around 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

Thus, in this article, we have understood the functionality of Python’s String ljust() and rjust() functions.


References

String ljust() and rjust() function

Leave a Reply

Your email address will not be published. Required fields are marked *