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

02 Feb, 2018
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.
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 stringprint ("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 fillchrprint ("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##########

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 stringprint ("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--------------------
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 stringprint ("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
Python-Built-in-functions
python-string
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?