AppDividend
Latest Code Tutorials

Python String Center Example | Python center() Method

1

Python String center method creates and returns a new string that is padded with the specified character. Python string center() method in python is used to provide padding with a specific character, which can be space or any other character. The method returns a new string that is padded.

Python String center()

The syntax of the center() method is the following.

string.center(width, fillchar)

The center() method takes two arguments:

width: length of the string with padded characters.
fillchar: padding character.

The fillchar argument is optional. If it’s not provided, space is taken as a default argument.

See the following example.

# app.py

s1 ='hello'
s2 = s1.center(7, '#')
print(s2)

See the output.

➜  pyt python3 app.py
#hello#
➜  pyt

It returns a string padded with the specified character. It doesn’t change the value of the main string; it stores the padded string in another variable.

#Programs on the method center()

Write a program to show the mechanism of the center() method.

# app.py

s1 = 'hello this is a string'
s2 = s1.center(40, "*")
print(s2)

See the output.

➜  pyt python3 app.py
*********hello this is a string*********
➜  pyt

Write a program in which use center() method and do padding with a special character “#”.

See the following code.

# app.py

string = "I am AlienX"
string2 = string.center(30, "#")
print(string2)

See the output.

➜  pyt python3 app.py
#########I am AlienX##########
➜  pyt

#Errors in Python center()

Before we conclude this post, let’s look at some error scenarios.

# app.py

string = "I am AlienX"
print(string.center('#'))

See the output.

➜  pyt python3 app.py
Traceback (most recent call last):
  File "app.py", line 2, in <module>
    print(string.center('#'))
TypeError: 'str' object cannot be interpreted as an integer
➜  pyt

We got the TypeError: ‘str’ object cannot be interpreted as an integer

Let’s see the following error example.

# app.py

string = "I am AlienX"
print(string.center())

See the output.

➜  pyt python3 app.py
Traceback (most recent call last):
  File "app.py", line 2, in <module>
    print(string.center())
TypeError: center() takes at least 1 argument (0 given)
➜  pyt

We got the TypeError: center() takes at least 1 argument (0 given)

The error occurred because we didn’t provide the length of the output string.

# app.py

string = "I am AlienX"
print(string.center(20, '#$'))

See the output.

➜  pyt python3 app.py
Traceback (most recent call last):
  File "app.py", line 2, in <module>
    print(string.center(20, '#$'))
TypeError: The fill character must be exactly one character long
➜  pyt

We got the TypeError: The fill character must be exactly one character long

The error occurred because we provide the multiple characters and not a single character of the output string.

Finally, Python center() Example is over.

Recommended Posts

Python String startswith()

Python string endswith()

Python string join()

Python string index()

Python string strip()

1 Comment
  1. Dhiraj pandya says

    Hey Admin, I was just wandering something about laravel and entered your site.
    Just as usual I was going through few of your article, and thanks, I got what I was looking for.
    Then I just thought of checking your site security a bit, then I noticed you are using wordpress and your site isn’t secure I mean your security is way too low. No any protection over admin login huh :/ and then I hit https://appdividend.com/users and I was able to get all your usersnames and that shouldn’t happen cause it could lead to some sorts of brute-force attacks. So you helped me on my thing so as a script kiddie and partly noob security researcher I also wanna give a bit some sorts of advice :). Protect your site site login with 2 factor authentication and litmit the login attempt as it could lead to bruteforce attacks and all, remove unnecessary plugins, and hide your wp-admin login by redirecting it over some other page using some plugins or manual code so that only you could access it.
    Also there are lot of things you guys need to fix up, for now that much should work good.
    Once again thank you for the article it helped me a lot 🙂
    Have a good day

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.