Python String center() MethodPython Functions

Difficulty Level:
Difficulty Level:

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.

Advertisement

© 2021 Pythonexample.org, all rights reserved. Privacy Policy | Contact Us