Python String center() Method

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

Syntax

string.center(width, fillchar)

Parameters

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.

Example 1

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

Output

#hello# 

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.

Example 2

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

print(s2)

Output

*********hello this is a string*********

Example 3

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

Output

#########I am AlienX########## 

That’s all!

1 thought on “Python String center() Method”

  1. 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

    Reply

Leave a Comment

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