Right Align String in Python

  • Post category:String

In Python, you can use the string format() method to right align string. In the below code, >30 is used to right align a string. The greater than sign is used to right align a string.

Step 1: Create a string

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

Step 2: Right 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