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:
Count Occurrences of a Char in a String
Python Program to Print all Prime Numbers in an Interval
Python Set issuperset()
Python Package
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Find LCM
Python len()
Python str()
Python Program to Convert Kilometers to Miles
Python Namespace and Scope
Map to String Conversion in Java
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python slice()
Python Set symmetric_difference()
Python staticmethod()
Intelligent Projects Using Python - Santanu Pattanayak
Python setattr()
Python sorted()
Python Object Oriented Programming
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Copy a File
Python String translate()
Python next()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Get Line Count of a File
Python Program to Create a Long Multiline String
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String join()
Convert char to String in Java
Python int()
Introduction to Scientific Programming with Python - Joakim Sundnes
Array to String Conversions