简体   繁体   English

如何在每一行中打印一个字符串,并从该行的第一行起留一些空格?

[英]How to print a string in each line with some space from the first of the line?

I did not write a clear subject because I didn't know what to write about it, sorry first of all.我没有写一个明确的主题,因为我不知道该写什么,首先抱歉。

What I want to reach is:我想要达到的是:

a
    a
        a
            a
                a
            a
        a
    a
a

With for loop I think I can reach this, but what I try is totally wrong.使用for循环,我想我可以达到这个目的,但我尝试的是完全错误的。

Your increment counter is useless in your code.您的increment计数器在您的代码中是无用的。

The way I would approach this is:我处理这个问题的方法是:

column_num = 0
for i in range(10):  # rows
    for j in range(10):  # cols
        if j == column_num:
            print("a", end='')
        else:
            print(' ', end='')
    print()  # newline
    column_num += 1

#then reverse:
column_num -= 1
for i in range(10): #rows
    for j in range(10): #cols
        if j == column_num:
            print("a", end = '')
        else:
            print(' ', end = '')
    print() #newline
    column_num -= 1

Maybe you are locking for something like this.也许你正在锁定这样的东西。

a = 'a    '
space = '\t'

for c in range(5):
    print(space*c + a)

for c in range(5,0,-1):
    print(space*c + a)

You can do it recursively:您可以递归地执行此操作:

def print_angle(s, start, end, direction):
    indent = "  "*start
    print(f"{indent}{s}")

    if start < end-1 and direction:
        print_angle(s, start+1, end, 1)
    elif start > 0:
        print_angle(s, start-1, end, 0)

print_angle('a', 0, 5, 1)

This prints:这打印:

a
   a
      a
        a
           a
        a
      a
   a
a

The idea here is:这里的想法是:

1. Pass the desired character to print - a in this case. 1.传所需的字符打印-一个在这种情况下。

2. Pass the starting point - 0 indents in this case. 2.通过起点 - 在这种情况下 0 缩进。

3. Pass the ending point - 5 total to go. 3.通过终点 - 共 5 关。

4. Pass direction in which to go - 1 up, 0 down. 4.传球方向 - 1 向上,0 向下。

5. Go recursively up until you reach the end, preserve direction, initially up. 5.递归向上直到到达终点,保持方向,最初向上。

6. Go recursively down until you reach 0 and exit. 6.递归向下直到到达 0 并退出。

暂无
暂无

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

相关问题 对于 python 字符串,如何将每一行打印为新行? - for python string how can I print each line as a new line? 如何在新行的列表中的单词的每个字母之间打印空格? - How to print a space between each letter of a word in a list on a new line? 如何在一行中的列表中打印每个字符串的首字母? - How do I print the first letter of each string in a list in one line? 如何从回溯堆栈中打印第一行 - How to print the first line from a traceback stack 如何替换文本文件中每行空格前的第一个数字 - How to replace first number before a space in each line in a text file 在字符串的新行中打印列表中的每个字典 - Print each dictionary from a list in a new line in a string 如果当前行包含字符串,如何从文件中打印下一行? - How to print the next line from a file, if current line contains a string? 如何逐行读取文件并在python的每一行中获取一些字符串 - How to read a file line by line and get a some string in each line in python 如何将第一行和最后两行中的字符串打印到python中的多个文本文件 - How to print string in the first and last TWO line to multiple textfile in python 我有一些文件格式的数据。 我想知道每一行的第一个字符串是什么,并计算它们重复了多少次 - I have some data in a file format. I want to know what is the first string in each line and count how many times they are repeated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM