Advertisement



< Prev
Next >



Python - String center() method



The center() string method is used to create a centrally aligned version of the invoked string, padded with a specific character on both of its sides, which is by default a space character.

Note : None of the string methods change the original string and only return a new string which reflects the change performed by the string method, hence the returned string must be stored or used(if needed to reflect the result), otherwise it is lost.




While centrally aligning the content of the invoked string, we can pad its content with a specific character of our choice, which is by default, a space character. Let us see an example

# 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, '.'))


Output is :


Original string is :  Smile Each Day!
############Smile Each Day!#############
XXXXXXXXXXXXSmile Each Day!XXXXXXXXXXXXX
000000000000Smile Each Day!0000000000000
@@@@@@@@@@@@Smile Each Day!@@@@@@@@@@@@@
............Smile Each Day!............. 





Please share this article -





< Prev
Next >
< String rindex() Method
String rjust() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement