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 time Module
Python Program to Shuffle Deck of Cards
Python frozenset()
Python Program to Merge Two Dictionaries
Python String isdigit()
Most commonly used String methods in Java
Python Program to Reverse a Number
Python Data Types
String Processing with Apache Commons Lang 3
Python Program to Trim Whitespace From a String
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Variables, Constants and Literals
Python Program to Randomly Select an Element From the List
Python Modules
Python Program to Convert Two Lists Into a Dictionary
Python Set isdisjoint()
Python staticmethod()
Python String splitlines()
Python *args and **kwargs
Python Dictionary
Python Program to Parse a String to a Float or Int
Python List remove()
Case-Insensitive String Matching in Java
Python Exception Handling Using try, except and finally statement
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python datetime
Python Program to Display Powers of 2 Using Anonymous Function
Python Dictionary get()
Converting a Stack Trace to a String in Java
Python len()
Python Set clear()