首页 > 解决方案 > 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?

标签: pythonpython-2.7ascii

解决方案


您可以使用该str.center方法。下面的代码假定屏幕宽度为 80 个字符:

s = '''  ________            ___                         
 /_  __/ /_  ___     /   |  ________  ____  ____ _
  / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/
 / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ / 
/_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/ '''
print('\n'.join(l.center(80) for l in s.splitlines()))

这输出:

                 ________            ___                                        
                /_  __/ /_  ___     /   |  ________  ____  ____ _               
                 / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/               
                / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ /                
               /_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/                 

推荐阅读