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 Set isdisjoint()
Adding a Newline Character to a String in Java
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python filter()
Python Set issubset()
Python String isdigit()
Python Program to Reverse a Number
Java InputStream to String
Python Program to Check Armstrong Number
Python Program to Delete an Element From a Dictionary
Python Program Read a File Line by Line Into a List
Python reversed()
Python Program to Check if a Number is Odd or Even
Python Program to Check if a Number is Positive, Negative or 0
Python String expandtabs()
Introduction to Scientific Programming with Python - Joakim Sundnes
Converting a Stack Trace to a String in Java
How to Remove the Last Character of a String?
Python String swapcase()
Intelligent Projects Using Python - Santanu Pattanayak
Python Set clear()
Python break and continue
Python sleep()
Python *args and **kwargs
Python String casefold()
Python Shallow Copy and Deep Copy
Python bin()
Python Program to Represent enum
Python List Comprehension
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Shuffle Deck of Cards
Python Program to Check Prime Number