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 super()
Python Set difference()
Python callable()
Jackson – Marshall String to JsonNode
Python RegEx
Python String capitalize()
Python slice()
Python Program to Find LCM
Python bool()
How to Get Started With Python?
Python Program to Compute the Power of a Number
Python Set update()
Convert Character Array to String in Java
Python Data Structures and Algorithms - Benjamin Baka
Python String replace()
Python Inheritance
Python Program to Represent enum
Python String rpartition()
Python Program to Trim Whitespace From a String
Python Program to Solve Quadratic Equation
Reading an HTTP Response Body as a String in Java
Python Program to Multiply Two Matrices
Python Set clear()
Python Program to Merge Two Dictionaries
Python Program to Concatenate Two Lists
Python Dictionary keys()
Python String rjust()
Python print()
Python Program to Convert Celsius To Fahrenheit
Python Program to Get Line Count of a File
Python Get Current time
Python Program to Find HCF or GCD