Python Center String: Align Text with Width

You can use the center() method of strings to center-align a string with a specified width. An example is shown below:

s = "hello"
width = 10
centered_string = s.center(width)
print(centered_string)

The result of the execution is:

  hello   

The center() method takes an integer parameter representing the width for center alignment, this method will add spaces on both sides of the string to make it display in the center.

bannerAds