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 Program to Compute the Power of a Number
Python String isprintable()
Case-Insensitive String Matching in Java
Python any()
Python String expandtabs()
Python List index()
Why String is Immutable in Java?
Python delattr()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python for Loop
Python Program to Get Line Count of a File
Python Program to Check If a List is Empty
Python @property decorator
Python Program to Find Factorial of Number Using Recursion
Python String isidentifier()
Deep Learning with Python - Francois Cholletf
Python Program to Shuffle Deck of Cards
Python Program to Access Index of a List Using for Loop
Python Program to Multiply Two Matrices
Python Tuple index()
Python Set discard()
Python strptime()
Python Program to Find HCF or GCD
Python while Loop
Most commonly used String methods in Java
Python break and continue
Python Program to Append to a File
Python Program to Merge Mails
Python Operators
Python id()
Python datetime
Python Custom Exceptions