Python - String Method - center() Tutorial
It will return the new string with center-aligned or equally padded on both ends using a specified character( or space by default).
Syntax-
String.center(length, fillchar)
#length - It is the total length of the string that needs to be returned
after filling specified character on both the end of actual string.
#fillchar - (Optional) It is specified character that need to be filled
on both the end of actual string. By default it is space
Example-
str1 = "Hello Python"
print(str1.center(30))
print(str1.center(30,'*'))
print(str1.center(2))
Output-
Hello Python
*********Hello Python*********
Hello Python
In the above program,
Line 2- str1 is equally centered from both the end with space as the default character, with the total length of the new string as 30
Line 3- str1 is equally centered from both the end with * as a specified character, with the total length of the new string as 30
Line 4- On providing length less than the original string. then it will return the original string.