[英]printing 2 stars using strings side by side
Without using the function call, how can I modify my string to have the stars come out as side by side?在不使用 function 调用的情况下,如何修改我的字符串以使星星并排出现?
This is what I did:这就是我所做的:
print(" *\n * *\n * *\n * *\n * * * * * *\n"
" * *\n * *\n **********" , end = "\n *\n * *\n * *\n * *\n * * * * * *\n"
" * *\n * *\n **********" )
but this results in the stars coming out as top and bottom.但这会导致星星出现在顶部和底部。 I want it to print side by side.我希望它并排打印。
#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)
* *
* * * *
* * * *
* * * *
* * * * * * * * * * * *
* * * *
* * * *
********** **********
You could use a triple quoted multi-line string to create an "arrow template" then utilize .splitlines()
and zip()
to print them side by side(with optional spacing between).您可以使用三引号多行字符串来创建“箭头模板”,然后利用.splitlines()
和zip()
并排打印它们(之间有可选的间距)。
UP_ARROW = """
*
* *
* *
* *
* * * * * *
* *
* *
**********
""".strip("\n").splitlines()
for arrows in zip(UP_ARROW, [" "] * len(UP_ARROW), UP_ARROW):
print(*arrows)
Output: Output:
* *
* * * *
* * * *
* * * *
* * * * * * * * * * * *
* * * *
* * * *
********** **********
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.