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.
cstr = "I love geeksforgeeks"
print ( "The original string is : \n" , cstr, "\n" )
print ( "The center aligned string is : " )
print (cstr.center( 40 ), "\n" )
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.
lstr = "I love geeksforgeeks"
print ( "The original string is : \n" , lstr, "\n" )
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.
rstr = "I love geeksforgeeks"
print ( "The original string is : \n" , rstr, "\n" )
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
Share your thoughts in the comments
Please Login to comment...