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:
Java InputStream to String
Python Program to Convert Two Lists Into a Dictionary
Python Program to Check If a List is Empty
Python String upper()
How to Get Started With Python?
Python Sets
Python Program to Find Numbers Divisible by Another Number
Python Set copy()
Python setattr()
Python String zfill()
Python Program to Print the Fibonacci sequence
Python Program to Check the File Size
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python String find()
Python bytes()
Python Program to Convert String to Datetime
Python List copy()
Python Program Read a File Line by Line Into a List
Java – Reader to String
Python Program to Print Output Without a Newline
Python Program to Convert Celsius To Fahrenheit
Python String rjust()
Python pass statement
Python frozenset()
Python String islower()
Python List insert()
Python Iterators
Python Program to Print all Prime Numbers in an Interval
Python format()
Python dict()
Python float()
Python Program to Count the Number of Digits Present In a Number