Articles → PYTHON → Center function in python

Center function in python






Purpose




  1. If value of width parameter is less than or equal to the number of characters in the string, then no filler will be added on either side of the string.
  2. If the value of width parameter is one more than the number of characters in the string, then filler will be added on the left-hand side of the string.
  3. If the value of width parameter is greater than number of characters in the string, then filler will be added on both side of the string.

Example


str = "this is string example"
print ("width is less than number of string characters -->", str.center(18, '*'))
print ("width is equal to number of string characters -->", str.center(22, '*'))
print ("width is one more than number of string characters -->", str.center(23, '*'))
print ("width is more than number of string characters -->", str.center(24, '*'))




Picture showing the output of center function in python

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, February 20, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250