[英]printing 2 stars using strings side by side
在不使用 function 调用的情况下,如何修改我的字符串以使星星并排出现?
这就是我所做的:
print(" *\n * *\n * *\n * *\n * * * * * *\n"
" * *\n * *\n **********" , end = "\n *\n * *\n * *\n * *\n * * * * * *\n"
" * *\n * *\n **********" )
但这会导致星星出现在顶部和底部。 我希望它并排打印。
#pass one arrow in a variable y
y = ''' *\n * *\n * *\n * *\n * * * * * *\n * *\n * *\n **********'''
#split on the linebreak
spl = y.split('\n')
#get width of the arrow
max_len = max([len(x) for x in spl])
#compute space for new arrow points as max width - position of last element
#of first arrow and repeat after space
new = [x + (max_len - len(x))*' ' +x for x in spl]
#join back on linebreaks
new = '\n'.join(new)
print(new)
* *
* * * *
* * * *
* * * *
* * * * * * * * * * * *
* * * *
* * * *
********** **********
您可以使用三引号多行字符串来创建“箭头模板”,然后利用.splitlines()
和zip()
并排打印它们(之间有可选的间距)。
UP_ARROW = """
*
* *
* *
* *
* * * * * *
* *
* *
**********
""".strip("\n").splitlines()
for arrows in zip(UP_ARROW, [" "] * len(UP_ARROW), UP_ARROW):
print(*arrows)
Output:
* *
* * * *
* * * *
* * * *
* * * * * * * * * * * *
* * * *
* * * *
********** **********
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.