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:
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Array to String Conversions
CharSequence vs. String in Java
Python Statement, Indentation and Comments
Python Program to Find Sum of Natural Numbers Using Recursion
Python Dictionary get()
Python format()
Python List Comprehension
Python Package
Python Program to Find the Sum of Natural Numbers
Python delattr()
Python pow()
Python Set intersection_update()
Python Program to Represent enum
Python issubclass()
Python Program to Illustrate Different Set Operations
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Check If Two Strings are Anagram
Python Objects and Classes
Python Operator Overloading
Python String casefold()
Python Program to Get the Last Element of the List
Python Exception Handling Using try, except and finally statement
Python String count()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python String isspace()
Case-Insensitive String Matching in Java
Python dir()
Python datetime
Python List index()
Python Program to Add Two Matrices
Intelligent Projects Using Python - Santanu Pattanayak