Python string alignment center method

Python String Alignment: center Method

Description

The center() method returns a centered string of a specified length. It is padded with the specified fill character, which defaults to spaces.

Syntax

The syntax of the center() method is as follows:

var.center(width[, fillchar])

Parameters

  • width – The total width of the string.
  • fillchar – The fill character.

Return Value

This method returns a string of at least width, created by padding the string with the character fillchar (which defaults to spaces).

Example

The following example demonstrates the use of the center() method.

var = "This Is String Example....WOW!!!"
var1 = var.center(40, '*')
print("Original string:", var)
print("Centered string:", var1)

When you run this program, it will produce the following output:

Original string: This Is String Example....WOW!!!
Centered string: ****This Is String Example....WOW!!!****

Leave a Reply

Your email address will not be published. Required fields are marked *