Table of Contents
The center() method returns a string which is padded with the specified character.
The syntax of center() method is:
string.center(width[, fillchar])
1. center() Parameters
The center() method takes two arguments:
- width – length of the string with padded characters
- fillchar (optional) – padding character
The fillchar argument is optional. If it’s not provided, space is taken as default argument.
2. Return Value from center()
The center() method returns a string padded with specified fillchar. It doesn’t modify the original string.
3. Example 1: center() Method With Default fillchar
string = "Python is awesome"
new_string = string.center(24)
print("Centered String: ", new_string)
Output
Centered String: Python is awesome
4. Example 2: center() Method With * fillchar
string = "Python is awesome"
new_string = string.center(24, '*')
print("Centered String: ", new_string)
Output
Centered String: ***Python is awesome****
Related posts:
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python String replace()
Python Matrices and NumPy Arrays
Python List extend()
Deep Learning with Python - Francois Chollet
Python Program to Find Numbers Divisible by Another Number
Python Program to Delete an Element From a Dictionary
Python Dictionary get()
Python Dictionary keys()
Python Machine Learning - Sebastian Raschka
Python divmod()
Python Shallow Copy and Deep Copy
Python if...else Statement
Python Program to Print Colored Text to the Terminal
Python any()
Python bool()
Python String title()
Python Program to Print Output Without a Newline
Python List Comprehension
Python Program to Transpose a Matrix
Python Exception Handling Using try, except and finally statement
Python Dictionary pop()
Java String Conversions
Python Program to Convert Two Lists Into a Dictionary
Python Objects and Classes
Python List
Python Program to Remove Punctuations From a String
Python repr()
Python String rstrip()
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Get the File Name From the File Path
Python dir()