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 bin()
Python Recursion
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python max()
Java InputStream to String
Python pass statement
Python Program to Shuffle Deck of Cards
Python Program to Get the File Name From the File Path
Python Tuple count()
Python hasattr()
Python Set remove()
Python Program to Trim Whitespace From a String
Python Input, Output and Import
Python String isdigit()
Python Program to Catch Multiple Exceptions in One Line
CharSequence vs. String in Java
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python String rjust()
Python classmethod()
Converting String to Stream of chars
Python float()
Python divmod()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
How to Remove the Last Character of a String?
Python Statement, Indentation and Comments
Why String is Immutable in Java?
Python strptime()
Python break and continue
Python ord()
Python Program to Check if a Number is Odd or Even
Python Program to Find Sum of Natural Numbers Using Recursion
String Initialization in Java