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 Program to Delete an Element From a Dictionary
Python Program to Capitalize the First Character of a String
Python Program to Merge Two Dictionaries
Python Set copy()
Python time Module
Python Program to Count the Number of Digits Present In a Number
Python Program to Copy a File
Python Program to Solve Quadratic Equation
Python Program to Illustrate Different Set Operations
Python Set symmetric_difference_update()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python classmethod()
Deep Learning in Python - LazyProgrammer
Python Data Types
Python Program to Access Index of a List Using for Loop
Python Program to Check If Two Strings are Anagram
JavaScript Methods of RegExp and String
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Global Keyword
Python Inheritance
Python set()
Python Program to Convert Bytes to a String
Python Dictionary keys()
Python Program to Get the Last Element of the List
Python format()
Python isinstance()
Python Program to Transpose a Matrix
Python Program to Reverse a Number
Python List count()
Python String rfind()
Python Set intersection_update()
Python Dictionary clear()