Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
edited by

How do I print it in the center? Is there a quick way to do this?

print('$s' % "MyString")     # prints right-aligned

print('%-24s' % "MyString")    # prints left aligned

I don't need the text to be in the middle of my screen. I only need to have it in the middle of those 24 spaces.

1 Answer

0 votes
by (107k points)
edited by

For achieving your result, you have to work with the new-style format function instead of the old-style % operator, which doesn't have the centering property:

print('{:^24s}'.format("MyString"))

If you are a total beginner and wish to learn Python, then do check out the below python tutorial video for better understanding:

Related questions

0 votes
1 answer
asked Nov 25, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...