Advertisement
center(length, padding)
Parameters | Description |
---|---|
length | This is the length of the string returned by the method center(). A non-optional attribute. |
padding | This value is used as a padding while centrally aligning the invoked string value. An optional attribute. |
# Python - Example of center() method
# Creating a string
s1 = ('new zealand')
# Printing the original string
print('Value of string : ', s1)
# Calling the center() method on string object
print('Calling the center() method on string : ', s1.center(20))
Value of string : new zealand
Calling the center() method on string : new zealand
# Python -Example of center() method
# Creating a string
s1 = ('Mars - The Red Planet')
# Printing the original string
print('Original string before calling center() : ', s1)
# Calling the center() method on string object
print('Calling the center() method : ', s1.center(40))
# Printing the original string
print('Original string after calling center() : ', s1)
Original string before calling center() : Mars - The Red Planet
Calling the center() method : Mars - The Red Planet
Original string after calling center() : Mars - The Red Planet
Advertisement
# Python - center() method example.
# Creating a string
s = 'Live each day the best you can!'
# Printing the original string
print('Original string is : ', s)
# Result of calling center() is made to reference by the reference of original string.
s = s.center(40)
# Old reference variable pointing to the new string
print('String value after center() method : ', s)
Original string is : Live each day the best you can!
String value after center() method : Live each day the best you can!
# Python - center() method example.
# Creating a string
s = 'Smile Each Day!'
# Printing the original string
print('Original string is : ', s)
# Calling the center() method on string object
# which will return a centrally aligned value padded with # on its both sides.
print(s.center(40, '#'))
# Calling the center() method on string object
# which will return a centrally aligned value padded with X on its both sides.
print(s.center(40, 'X'))
# Calling the center() method on string object
# which will return a centrally aligned value padded with 0 on its both sides.
print(s.center(40, '0'))
# Calling the center() method on string object
# which will return a centrally aligned value padded with @ on its both sides.
print(s.center(40, '@'))
# Calling the center() method on string object
# which will return a centrally aligned value padded with @ on its both sides.
print(s.center(40, '.'))
Original string is : Smile Each Day!
############Smile Each Day!#############
XXXXXXXXXXXXSmile Each Day!XXXXXXXXXXXXX
000000000000Smile Each Day!0000000000000
@@@@@@@@@@@@Smile Each Day!@@@@@@@@@@@@@
............Smile Each Day!.............
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement