Python String center() Method

Advertisements


Description

The method center() returns centered in a string of length width. Padding is done using the specified fillchar. Default filler is a space.

Syntax

Following is the syntax for center() method:

str.center(width[, fillchar])

Parameters

  • width -- This is the total width of the string.

  • fillchar -- This is the filler character.

Return Value

This method returns centered in a string of length width.

Example

The following example shows the usage of center() method.

#!/usr/bin/python

str = "this is string example....wow!!!";

print "str.center(40, 'a') : ", str.center(40, 'a')

When we run above program, it produces following result:

str.center(40, 'a') :  aaaathis is string example....wow!!!aaaa



Advertisements
Advertisements