Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am creating a simple console-based Python (version 2.4) script that will be run on a Bash shell. It is a simple menu-based script that displays a set of options to the user and will do specific tasks depending on the user input.

When the script starts, I want it to clear the screen and display the menu in the center of the screen. I can get the console width from stty size. How do I go about center-aligning the text?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
943 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use str.center

s = "hello world"
s.center(40)
>>> '              hello world               '

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...