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:
Most commonly used String methods in Java
Python String count()
Deep Learning with Python - Francois Chollet
Check if a String is a Palindrome in Java
Python Program to Slice Lists
Python Program to Find Armstrong Number in an Interval
Python Dictionary values()
Python Program to Find All File with .txt Extension Present Inside a Directory
Count Occurrences of a Char in a String
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Dictionary copy()
Python print()
Python Keywords and Identifiers
Python map()
Python String translate()
Python Set copy()
Python Set symmetric_difference()
Java Program to Permute All Letters of an Input String
Python Variables, Constants and Literals
Python Program to Find Numbers Divisible by Another Number
Python Shallow Copy and Deep Copy
Python String isalpha()
Python Input, Output and Import
Python Dictionary clear()
Python Program to Create a Countdown Timer
Python Operators
How to Remove the Last Character of a String?
Java – Generate Random String
Python filter()
Python RegEx
Python Program to Compute all the Permutation of the String
Convert Character Array to String in Java