Open In App

String rjust() and ljust() in python()

Last Updated : 08 Jan, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

1. String rjust() The string rjust() method returns a new string of given length after substituting a given character in left side of original string.

Syntax:

string.rjust(length, fillchar)

Parameters:

length: length of the modified string. If length is less than or equal to the length of the original string then original string is returned.
fillchar: (optional) characters which needs to be padded. If it’s not provided, space is taken as a default argument.

Returns:

Returns a new string of given length after substituting a given character in left side of original string.

Example




# Python program to demonstrate working of 
# rjust()
string = 'geeks'
length = 8
  
# If no fill character is provided, space
# is used as fill character
print(string.rjust(length))


Output:

   geeks

Example




# example string
string = 'geeks'
length = 8
fillchar = '*'
  
print(string.rjust(length, fillchar))


Output:

***geeks  

 

2. String ljust()
The string ljust() method returns a new string of given length after substituting a given character in right side of original string.

Syntax:

string.ljust(length, fillchar)

Parameters:

length: length of the modified string. If length is less than or equal to the length of the original string then original string is returned.
fillchar: (optional) characters which needs to be padded. If it’s not provided, space is taken as a default argument.

Returns:

Returns a new string of given length after substituting a given character in right side of original string.

Example 1




# example string
string = 'geeks'
length = 8
  
# If no fill character is provided, space
# is used as fill character.
print(string.ljust(length))


Output: (Three spaces are printed after geeks)

geeks   

Example 2




# example string
string = 'geeks'
length = 8
fillchar = '*'
  
# print left justified string
print(string.ljust(length, fillchar))


Output:

geeks***  


Similar Reads

Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
Some of the string methods are covered in the set 3 below String Methods Part- 1 More methods are discussed in this article 1. len() :- This function returns the length of the string. 2. count("string", beg, end) :- This function counts the occurrence of mentioned substring in whole string. This function takes 3 arguments, substring, beginning posi
4 min read
Python String | ljust(), rjust(), center()
String alignment is frequently used in many day-day applications. Python in its language offers several functions that helps to align string. Also, offers a way to add user specified padding instead of blank space. These functions are : str.ljust(s, width[, fillchar]) str.rjust(s, width[, fillchar]) str.center(s, width[, fillchar]) These functions
3 min read
Python | Pandas Series.str.ljust() and rjust()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas .ljust() and .rjust() are Text methods used to deal with text data in series. Since these methods are applicable only on strings,
3 min read
Python String rjust() Method
Python String rjust() method returns a new string of a given length after substituting a given character on the left side of the original string. Python String rjust() Method Syntax Syntax: string.rjust(length, fillchar) Parameters: length: length of the modified string. If length is less than or equal to the length of the original string then orig
2 min read
Python String ljust() Method
Python String ljust() method left aligns the string according to the width specified and fills the remaining space of the line with blank space if ‘fillchr‘ argument is not passed. Python String ljust() Method Syntax: Syntax: ljust(len, fillchr) Parameters: len: The width of string to expand it.fillchr (optional): The character to fill in the remai
2 min read
numpy string operations | rjust() function
numpy.core.defchararray.rjust(arr, width, fillchar=' ') is another function for doing string operations in numpy. It returns an array with the elements of arr right-justified in a string of length width.It fills remaining space of each array element using fillchr parameter.If fillchr is not passed then it fills remaining spaces with blank space. Pa
2 min read
numpy string operations | ljust() function
numpy.core.defchararray.ljust(arr, width, fillchar=' ') is another function for doing string operations in numpy. It returns an array with the elements of arr left-justified in a string of length width. It fills remaining space of each array element using fillchr parameter. If fillchr is not passed then it fills remaining spaces with blank space. P
2 min read
String to Int and Int to String in Python
Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have to use the int() function. This function is used
2 min read
Python - Create a string made of the first and last two characters from a given string
Here we are going to see the approach of forming a string made from the first and last 2 characters of a given string. Input: Geeksforgeeks Output: Geks Input: Hi, There Output: Hire Method #1: Using list slicing In this example, we are going to loop through the string and store the length of the string in the count variable and then make the new s
3 min read
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
Article Tags :
Practice Tags :