Saturday, November 16, 2024
Google search engine
HomeLanguagesPython String | ljust(), rjust(), center()

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.




# Python3 code to demonstrate 
# the working of center()
  
cstr = "I love neveropen"
  
# 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 neveropen 

The center aligned string is : 
          I love neveropen           

Center aligned string with fillchr: 
##########I love neveropen##########
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 neveropen"
  
# 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 neveropen 

The left aligned string is : 
I love neveropen--------------------
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 neveropen"
  
# 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 neveropen 

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

RELATED ARTICLES

Most Popular

Recent Comments