Open In App

Python String | ljust(), rjust(), center()

Improve
Improve
Like Article
Like
Save
Share
Report

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 respectively left-justify, right-justify and center a string in a field of given width. They return a string that is at least width characters wide, created by padding the string s with the character fillchar (default is a space) until the given width on the right, left or both sides. The string is never truncated.


center()

This function center aligns the string according to the width specified and fills remaining space of line with blank space if ‘ fillchr ‘ argument is not passed.

Syntax :
center( len, fillchr )

Parameters :
len : The width of string to expand it.
fillchr (optional): The character to fill in remaining space.

Return Value :
The resultant center aligned string expanding the given width.




# Python3 code to demonstrate 
# the working of center()
  
cstr = "I love geeksforgeeks"
  
# Printing the original string
print ("The original string is : \n", cstr, "\n")
  
# Printing the center aligned string 
print ("The center aligned string is : ")
print (cstr.center(40), "\n")
  
# Printing the center aligned 
# string with fillchr
print ("Center aligned string with fillchr: ")
print (cstr.center(40, '#'))


Output :

The original string is : 
 I love geeksforgeeks 

The center aligned string is : 
          I love geeksforgeeks           

Center aligned string with fillchr: 
##########I love geeksforgeeks##########


ljust()

This function left aligns the string according to the width specified and fills remaining space of line with blank space if ‘ fillchr ‘ argument is not passed.

Syntax :
ljust( len, fillchr )

Parameters :
len : The width of string to expand it.
fillchr (optional): The character to fill in remaining space.

Return Value :
The resultant left aligned string expanding the given width.




# Python3 code to demonstrate 
# the working of  ljust()
  
lstr = "I love geeksforgeeks"
  
# Printing the original string
print ("The original string is : \n", lstr, "\n")
  
# Printing the left aligned 
# string with "-" padding 
print ("The left aligned string is : ")
print (lstr.ljust(40, '-'))


Output :

The original string is : 
 I love geeksforgeeks 

The left aligned string is : 
I love geeksforgeeks--------------------


rjust()

This function right aligns the string according to the width specified and fills remaining space of line with blank space if ‘ fillchr ‘ argument is not passed.

Syntax :
rjust( len, fillchr )

Parameters :
len : The width of string to expand it.
fillchr (optional) : The character to fill in remaining space.

Return Value :
The resultant right aligned string expanding the given width.




# Python3 code to demonstrate 
# the working of rjust()
  
rstr = "I love geeksforgeeks"
  
# Printing the original string
print ("The original string is : \n", rstr, "\n")
  
# Printing the right aligned string
# with "-" padding 
print ("The right aligned string is : ")
print (rstr.rjust(40, '-'))


Output :

The original string is : 
 I love geeksforgeeks 

The right aligned string is : 
--------------------I love geeksforgeeks



Last Updated : 02 Feb, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads