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 for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Count the Number of Each Vowel
Python List append()
Python Dictionary update()
Python Program to Split a List Into Evenly Sized Chunks
Python frozenset()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Create a Countdown Timer
Python float()
Python dict()
Python Program to Display Calendar
Python hasattr()
Python Global, Local and Nonlocal variables
Python any()
Python Program to Convert Decimal to Binary Using Recursion
Python Multiple Inheritance
Python Program to Parse a String to a Float or Int
String Hashing
Python String isprintable()
Split a String in Java
Python Program to Print all Prime Numbers in an Interval
Java – String to Reader
Python Program to Make a Simple Calculator
Python Program to Find Sum of Natural Numbers Using Recursion
Intelligent Projects Using Python - Santanu Pattanayak
Machine Learning with Python for everyone - Mark E.Fenner
Python ord()
Most commonly used String methods in Java
Python Modules
Python Program to Print Colored Text to the Terminal
Python Input, Output and Import
Python map()