Center Alignment of String in Python

  • Post category:String

The center alignment of string can be done in Python using the string format() method. In the below code, ^30 is used to center align a string. The caret (^) is used to center align a string.

Step 1: Create a string

str = "CodeAllow"
print("str = ", str)
print("str type = ", type(str))

Step 2: Center align a string

print("str = ", "{:^30}".format(str))

Output:

str =  CodeAllow
str type =  <class 'str'>
str =            CodeAllow           

Free resources to learn advanced skills: AiHints and CodeAllow