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:
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python File I/O Operation
Python bin()
Python String rpartition()
Python String maketrans()
JavaScript Methods of RegExp and String
Python format()
Python Set symmetric_difference()
Python zip()
Python iter()
Python Package
Python max()
Python Program to Randomly Select an Element From the List
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Dictionary get()
Python Set intersection()
Python globals()
Python Matrices and NumPy Arrays
Python Program to Find All File with .txt Extension Present Inside a Directory
Converting a List to String in Java
Python Dictionary clear()
Case-Insensitive String Matching in Java
Python dict()
Python Set symmetric_difference_update()
Python slice()
Java InputStream to String
Python Program to Remove Punctuations From a String
Python Program to Solve Quadratic Equation
Python Functions
Python if...else Statement
Python Set issubset()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...