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 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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, '#'))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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, '-'))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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, '-'))

chevron_right


Output :

The original string is : 
 I love geeksforgeeks 

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.