Change language

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 

Python String | simple (), simple (), center () Python functions: Questions

Python String | simple (), simple (), center () String Variables: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method