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 del Statement
Node.js vs Python for Backend Development
Python String isalpha()
Python Program to Trim Whitespace From a String
Python input()
Python String rstrip()
Python Program to Return Multiple Values From a Function
Python String isspace()
Python String count()
Python Program to Check if a Number is Positive, Negative or 0
Python @property decorator
Map to String Conversion in Java
JavaScript Methods of RegExp and String
Python dict()
Python Strings
Adding a Newline Character to a String in Java
Python memoryview()
Java Program to Permute All Letters of an Input String
Python String translate()
Python List clear()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python round()
Check if a String is a Palindrome in Java
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Convert Decimal to Binary Using Recursion
Python String isdigit()
Python Matrices and NumPy Arrays
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Copy a File
Python staticmethod()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python getattr()