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 Create Pyramid Patterns
Python Program to Make a Flattened List from Nested List
Python if...else Statement
Python Program to Count the Number of Each Vowel
Python Program to Compute the Power of a Number
Python Generators
Python Program to Check Whether a String is Palindrome or Not
Python Global Keyword
Python Dictionary popitem()
Python help()
Python delattr()
Python Program to Find ASCII Value of Character
Java – String to Reader
Adding a Newline Character to a String in Java
Python max()
Python time Module
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Compute all the Permutation of the String
Python datetime
Python Program to Access Index of a List Using for Loop
Python bin()
Python enumerate()
Python String islower()
Python Program to Convert Two Lists Into a Dictionary
Python String strip()
How to Remove the Last Character of a String?
Case-Insensitive String Matching in Java
Python setattr()
Python Inheritance
Python Program to Add Two Numbers
Python List index()