Python String | simple (), simple (), center ()



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 the string at the specified width. They return a string that is at least characters wide, created by filling the string s with fillchar (which is blank by default) to the specified width on the right, left, or both sides. The line is never truncated.

center()

This functional center aligns the line to match with the specified width and fills the remaining line space with a space if the argument & # 39;  fillchr & # 39; not transmitted.

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 demo code
# center () work

 

cstr = "I love pythonengineering"

 
# Print original line

print ( "The original string is: " , cstr, " " )

 
# Print line centered

print ( "The center aligned string is:" )

print (cstr.center ( 40 ), " " )

 
# Center printing
# line with fillchr

print ( " Center aligned string with fillchr: " )

print (cstr.center ( 40 , `#` ))

Output:

 The original string is : I love pythonengineering The center aligned string is: I love pythonengineering Center aligned string with fillchr: ########## I love pythonengineering ########## 

ljust ()

This function aligns a string to the specified width left and fills the remaining line space with a space if the argument & # 39;  fillchr & # 39; not transmitted.

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 demo code
# ljust () work

 

lstr = " I love pythonengineering "

  
# Print original line

print ( "The origi nal string is: " , lstr, " " )

 
# Left-aligned
# line with the addition & quot; - & quot;

print ( "The left aligned string is:" )

print (lstr.ljust ( 40 , `-` ))

Output:

 The original string is: I love pythonengineering The left aligned string is: I love pythonengineering --------------- ----- 

rjust ()

This function aligns a string to the specified width and fills in the rest request string transfer with a space if the argument & # 39;  fillchr & # 39; not transmitted.

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 demo code
# running rjust ()

 

rstr = "I love pythonengineering"

 
# Print original line

print ( "The original s tring is: " , rstr, " " )

 
# Print the line to the right
# with & quot; - & quot; addition

print ( "The right aligned string is: " )

print ( rstr.rjust ( 40 , `-` ))

Output:

 The original string is: I love pythonengineering The right aligned string is: -------------------- I love pythonengineering