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 strftime()
Python String lstrip()
Python callable()
Python int()
Python vars()
Python Program to Return Multiple Values From a Function
Python Strings
Python Dictionary setdefault()
Converting String to Stream of chars
Python map()
Python Set symmetric_difference()
Python Program to Add Two Numbers
Python Dictionary get()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python for Loop
Python Program to Count the Occurrence of an Item in a List
Python list()
Python Program to Check Prime Number
Python Program to Convert Bytes to a String
Python isinstance()
Python Program to Count the Number of Each Vowel
Python Data Structures and Algorithms - Benjamin Baka
Python complex()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python String splitlines()
Python property()
Python String isdecimal()
Python Program to Merge Two Dictionaries
Python bin()
Python Program to Find HCF or GCD
Python Program to Sort a Dictionary by Value
Generate a String