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 Closures
Python int()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Program to Iterate Over Dictionaries Using for Loop
Python Dictionary fromkeys()
Convert Character Array to String in Java
Python Set intersection()
Python Program to Concatenate Two Lists
Python Deep Learning Cookbook - Indra den Bakker
How to Remove the Last Character of a String?
Python List
Python filter()
Python List extend()
Python float()
Python hash()
Python enumerate()
Python List sort()
How to get current date and time in Python?
Python String rfind()
Java – Reader to String
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python ascii()
Python Dictionary
Converting a Stack Trace to a String in Java
Adding a Newline Character to a String in Java
Python hex()
Python String startswith()
Map to String Conversion in Java
Python String rindex()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Node.js vs Python for Backend Development
Python frozenset()