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:
Python any()
Python Deep Learning Cookbook - Indra den Bakker
Python String format()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python filter()
Python Function Arguments
Python Set intersection()
Python Program to Check If a String Is a Number (Float)
Python frozenset()
Python Program to Display Powers of 2 Using Anonymous Function
Python str()
Python Generators
Python String title()
Python Program to Multiply Two Matrices
Python int()
Python vars()
Python exec()
Python Operators
Python Program to Sort a Dictionary by Value
Python String lstrip()
Python callable()
Python String expandtabs()
Python List remove()
Python set()
Python String istitle()
Python Errors and Built-in Exceptions
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
JavaScript Methods of RegExp and String
Python Program to Merge Two Dictionaries
Python String rsplit()
Python __import__()