简体   繁体   English

如何最小化嵌套循环中字符之间的空间?

[英]How do I minimize the space between characters in my nested loop?

so I'm trying to get my nested loop to display an image like in this picture:所以我试图让我的嵌套循环显示如下图所示的图像:

图片

So far, this is the code that I have.到目前为止,这是我拥有的代码。

for a in range (1):
    print("#""#")
    for b in range (0,5,1):
        print("#", end=" ")
        for c in range(b):
            print(" ", end=" ")
        print("#")

I'm new to the site, so please excuse my terrible formatting.我是该网站的新手,所以请原谅我糟糕的格式。 The output I'm getting seems to have an extra space per line compared to the image given, and I'm not sure how to get rid of the space.与给定的图像相比,我得到的 output 似乎每行都有额外的空间,我不知道如何摆脱空间。 I'd appreciate any help!我会很感激任何帮助!

I'm thinking it's the 'end=' '' statement, but if I try replacing that with just a space, my entire line goes wonky.我认为这是 'end=' '' 声明,但如果我尝试用一个空格替换它,我的整个行都会变得不稳定。

Thanks!谢谢!

end=" " prints an space instead of a newline in the end.. end=" "打印一个空格而不是换行符..

I think its better to concatenate the string in this case instead of manipulating the print's end..我认为在这种情况下连接字符串而不是操纵打印的结尾更好。

for i in range(5):
    print('#' + ' '*i + '#')

output: output:

##
# #
#  #
#   #
#    #

Like this?像这样? Changed the third print更改了第三次打印

for a in range (1):
    print("#""#")
    for b in range (0,5,1):
        print("#", end=" ")
        for c in range(b):
            print(end=" ")
        print("#")

You need to remove the whitespace in the 2nd end variable您需要删除第二个结束变量中的空格

for a in range (1):
print("#""#")
for b in range (0,5,1):
    print("#", end=" ")
    for c in range(b):
        print(" ", end="") #this end variable is what is causing your additional space
    print("#")

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

相关问题 满足条件时如何在字符之间添加空格? - How do I add a space between characters when a condition is met? 如何使用 python 删除循环 output 之间的空间? - How do I delete space between loop output using python? 打印时如何删除列表字符之间的空格? - How do I remove space in between list characters when I print? 如何返回代码的开头/具有嵌套循环? - How do i return to the beginning of my code/have a nested loop? 如何在从 FOR LOOP 获得的每个数据之间放置一个空格 - How do I put a space between every data got from the FOR LOOP 如何使用循环中的 print(), 语句去除 output 中字符之间的空格? - How do I get rid of the spaces between the characters in the output using a print(), statement in a loop? 如何在 Y 轴和 Matplotlib 中的开始和结束垂直条之间留出一些空间 - How to do I give some space between Y axis and my starting and ending vertical bars in Matplotlib Python 如何在更新函数问题中的变量之间放置一个空格 - Python How do I put a space in between my variable in an update function problem 如何在两个单词之间留有空格来命名我的列表? - How do I name my list with having space in between two words? 如何在 python 中的特殊字符周围创建空格? - How do I create a space around special characters in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM