Python 3 - String center() Method


Advertisements

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 a string that is at least width characters wide, created by padding the string with the character fillchar (default is a space).

Example

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

#!/usr/bin/python3

str = "this is string example....wow!!!"
print ("str.center(40, 'a') : ", str.center(40, 'a'))

Result

When we run above program, it produces the following result −

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

Useful Video Courses


Video

Python Online Training

187 Lectures 17.5 hours

Malhar Lathkar

Video

Python Essentials Online Training

55 Lectures 8 hours

Arnab Chakraborty

Video

Learn Python Programming in 100 Easy Steps

136 Lectures 11 hours

In28Minutes Official

Video

Python with Data Science

Best Seller

75 Lectures 13 hours

Eduonix Learning Solutions

Video

Python 3 from scratch to become a developer in demand

Best Seller

70 Lectures 8.5 hours

Lets Kode It

Video

Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

Abhilash Nelson

Advertisements