python - ASCII Characters Text Align
问题描述
I'm coding a very small and basic text based adventure game in python, and I want to be able to align my title (which is in ASCII) I've looked it up but I either didn't understand it or it didn't work.
Here is the ASCII:
________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/
(It's not really creative, I know)
I want to get it to align to the center, is that possible?
解决方案
您可以使用该str.center
方法。下面的代码假定屏幕宽度为 80 个字符:
s = ''' ________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/ '''
print('\n'.join(l.center(80) for l in s.splitlines()))
这输出:
________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/
推荐阅读
- github - Github 桌面提交问题
- javascript - 创建图库时如何解决全局变量问题?
- go - 如何处理go中的多个错误?
- matlab - 如何在 Matlab 中显示具有多个特征的 SVM 图
- html - 如何在桌面视图中显示 unicode
- python - 将图例添加到 plotWidget 时 CSVexporter 失败
- c# - XAML 中的 String.Format 与 Xamarin 表单中的本地化
- java - 刷新 Apache Beam/Dataflow 中的边输入
- css - 嵌套 flex 的 Edge+Firefox 高度问题
- python - 多处理:字典不是持久的