string center() in python

The center() method creates and returns a new string which is padded with the specified character.

Syntax:

string.center(length[, fillchar])
Parameters:
length: length of the string after padding with the characters.
fillchar:(optional) characters which needs to be padded. 
If it's not provided, space is taken as default argument.
Returns:
returns a string padded with specified fillchar and it doesn't modify 
the original string.

CODE 1



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to illustrate
# string center() in python
string = "geeks for geeks"
  
new_string = string.center(24)
  
# here filchar not provided so takes space by default.
print "After padding String is: ", new_string

chevron_right


Output:

After padding String is:      geeks for geeks

CODE 2

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to illustrate
# string center() in python
string = "geeks for geeks"
  
new_string = string.center(24, '*')
  
# here fillchar is provided
print "After padding String is:", new_string

chevron_right


Output:

After padding String is: ****geeks for geeks*****


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.