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 float() Built in Function

Python String encode() Method

Python getattr() Built in Function

Python Use Get Method Over Square Brackets

Python exec() Built in Function