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 float()
Python String expandtabs()
Python Set intersection_update()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Inheritance
Python Multiple Inheritance
Python isinstance()
Python Dictionary
Python String title()
Python bytes()
Python Program to Iterate Through Two Lists in Parallel
Python Dictionary setdefault()
Python String maketrans()
Python Dictionary fromkeys()
Python dir()
Python Program to Find the Size (Resolution) of a Image
Python Input, Output and Import
Python Tuple count()
Python Operator Overloading
Python String rsplit()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Set remove()
Python List extend()
Python Iterators
Why String is Immutable in Java?
Python Sets
String Initialization in Java
Python Set isdisjoint()
Python object()
Python String format_map()
Python Set difference()
Python for Loop