简体   繁体   English

尝试在同一行中打印 ascii 文本 python 3.7

[英]trying to print ascii text in same line python 3.7

Im trying to write a program that takes a letter as input and prints the corresponding letter in ascii art.我正在尝试编写一个程序,该程序将一个字母作为输入并在 ascii 艺术中打印相应的字母。 I try to do a print() statement but it prints in seperate lines and not the same line which i want.我尝试执行 print() 语句,但它打印在单独的行中,而不是我想要的同一行。

 letter_I = """
 ###
  #
  #
  #
 ###
"""

letter_o = """
 ###
#   #
#   #
#   #
 ###
"""



print(letter_I + letter_o)

I want the input to be我希望输入是

###       ###
 #       #   #
 #       #   #
 #       #   #
###       ###

But i get但我明白了

 ###
  #
  #
  #
 ###


 ###
#   #
#   #
#   #
 ###

How do i get the preferred output, and is it possible to print it mirrored without messing up the letters?我如何获得首选输出,是否可以在不弄乱字母的情况下进行镜像打印?

Use this function使用这个功能

def combineStr(str1, str2):
    l1 = str1.split('\n')
    l2 = str2.split('\n')
    for i in range(min(len(l1), len(l2))):
        print(l1[i]+'\t'+l2[i]);

So you can do this:所以你可以这样做:

combineStr(letter_I, letter_o)

You can do it manually by splitting each strings line by line, and printing each line followed by a fixed number of spaces.您可以通过逐行拆分每个字符串并打印每行后跟固定数量的空格来手动执行此操作。

letter_I = """
 ###
  #
  #
  #
 ###
"""

letter_o = """
 ###
#   #
#   #
#   #
 ###
"""

letter_I = letter_I.split("\n")
letter_o = letter_o.split("\n")

for i in range(len(letter_I)):
    print(letter_I[i].ljust(7), letter_o[i])

In one hand you could use ANSI terminal escape sequences (see for example: https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html ), but in this usecase i think it would be rather painful.一方面,您可以使用 ANSI 终端转义序列(参见例如: https : //www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html ),但在这个用例中,我认为这会很痛苦。 Instead try to write the text by "pixel" lines, so first print the upmost "pixels" from each character, and then jumpt to the next row.而是尝试按“像素”行写入文本,因此首先打印每个字符最上面的“像素”,然后跳到下一行。 For this of course you need to know the whole text in advance.为此,您当然需要提前了解全文。 On the other other side, of course there is a lib for that, look at https://github.com/pwaller/pyfiglet另一方面,当然有一个库,看看https://github.com/pwaller/pyfiglet

You have to split the letters into individual lines and print one line of all letters before printing the next line.在打印下一行之前,您必须将字母分成单独的行并打印所有字母中的一行。

letters = [letter_I, letter_o]
for lines in zip(*map(str.splitlines, letters)):
    print(*(line.ljust(5) for line in lines))

Here, map applies the splitlines method to each letter and then zip arranges them in the right way.在这里, mapsplitlines方法应用于每个字母,然后zip以正确的方式排列它们。 ljust makes all lines the same width before printing them. ljust使所有行在打印之前具有相同的宽度。 (Alternatively, you could just print("\\t".join(lines)) but that gives you less control of the spacing.) (或者,您可以只print("\\t".join(lines))但这可以减少对间距的控制。)

Output:输出:

 ###   ### 
  #   #   #
  #   #   #
  #   #   #
 ###   ### 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM