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:
Generate a String
Python globals()
Python Keywords and Identifiers
Python Numbers, Type Conversion and Mathematics
Python staticmethod()
Python Program to Find the Square Root
Python Program to Represent enum
Python Program to Capitalize the First Character of a String
Python Program to Display Powers of 2 Using Anonymous Function
Python Set discard()
Python Program to Differentiate Between type() and isinstance()
Python Program to Check if a Number is Positive, Negative or 0
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Format ZonedDateTime to String
Java InputStream to String
Java String to InputStream
Python String rindex()
Python List reverse()
Python Set remove()
Python Tuple index()
Python Program to Swap Two Variables
Python Program to Generate a Random Number
Python String isidentifier()
Python String islower()
Python Dictionary get()
Python List index()
Python Program to Add Two Matrices
APIs in Node.js vs Python - A Comparison
String Set Queries
Python Program to Print the Fibonacci sequence
Python Program to Check the File Size
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman