Articles → PYTHON → Center Function In Python
Center Function In Python
Purpose
- If the value of the 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
- If the value of the 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
- If the value of the width parameter is greater than the number of characters in the string, then filler will be added on both sides 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, '*'))
Output