How to Use String center() Method in PythonPython Basics


The center() method returns a new string that is padded with the specified fillchar.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-


str = "aaa"
print ("str.center(10,'*'): ", str.center(10,'*'))

str = "hello python"
print ("str.center(10,'*'): ", str.center(20,'*'))

Output:

str.center(10,'*'):  ***aaa****
str.center(10,'*'):  ****hello python****

Syntax

str.center(width[, fillchar])

Parameters

Name Description
width The total width of the string
fillchar Filled characters

Return Value

It returns a string padded with specified fillchar.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us