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 id()
Adding a Newline Character to a String in Java
Python File I/O Operation
Python Dictionary values()
Python filter()
Converting String to Stream of chars
Python String isalnum()
Python Keywords and Identifiers
Python ascii()
Python datetime
Python Set copy()
Python String index()
Python Dictionary clear()
Python sum()
Python Program to Display the multiplication Table
Python strptime()
Python issubclass()
Python pow()
Python Program to Find All File with .txt Extension Present Inside a Directory
String Processing with Apache Commons Lang 3
Python Program to Capitalize the First Character of a String
Python Errors and Built-in Exceptions
Python Program to Check If Two Strings are Anagram
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Print Output Without a Newline
Python Program to Safely Create a Nested Directory
Python len()
Python Set clear()
Python String rstrip()
Python Dictionary fromkeys()
Python List sort()
Why String is Immutable in Java?