Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
935 views
in Technique[技术] by (71.8m points)

shell - Center-aligning text on console in Python

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

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

1 Answer

0 votes
by (71.8m points)

You can use str.center

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...