String center () in Python

Python Methods and Functions | String Variables

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

# Python program for illustration
# line center () in python

string = "geeks for geeks"

 

new_string = string.center ( 24 )

 
# filchar is not specified here, so takes up space by default.

print "After padding String is:" , new_string

Output:

 After padding String is: geeks for geeks  

Code 2

# Python program for illustration
# line center () in python

string = "geeks for geeks"

 

new_string = string.center ( 24 , '*' )

 
# fillchar is provided here

print "After padding String is:" , new_string

Output:

 After padding Str ing is: **** geeks for geeks ***** 




Tutorials