Python String center() Method

The Python center() method is a string method that returns a new string that is padded with a specified character (space by default) on both sides of the original string. The length of the new string is specified by a parameter.

Here is an example of how to use the center() method to align a string with 20 characters and use # as the padding character:


str = "Hello Javatpoint"
str2 = str.center(20,'#')
print("Old value:", str)
print("New value:", str2)

Output:
Old value: Hello Javatpoint
New value: ##Hello Javatpoint##

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Stock Market Predictions with LSTM in Python

Collections In Python

Python Count Occurrence Of Elements

Python One Liner Functions