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 intersection_update()
Python hasattr()
Python Program to Differentiate Between del, remove, and pop on a List
Python bool()
Case-Insensitive String Matching in Java
Python Program to Access Index of a List Using for Loop
Python Program to Copy a File
Python tuple()
Python Program to Find Factorial of Number Using Recursion
Python Dictionary clear()
Python String rsplit()
Python for Loop
Python Program to Convert String to Datetime
Introduction to Scientific Programming with Python - Joakim Sundnes
Python String lower()
Python Program to Check if a Number is Odd or Even
Python Program to Convert Kilometers to Miles
Python Program to Compute all the Permutation of the String
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Custom Exceptions
Python List extend()
Adding a Newline Character to a String in Java
Python Program to Print Hello world!
Python Shallow Copy and Deep Copy
Python String strip()
Python Global Keyword
Converting a List to String in Java
Python repr()
Python Input, Output and Import
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Find the Factors of a Number
Python next()