gpt4 book ai didi

python - 如何使用 print 并且不在参数之间添加空格?

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:32 34 4
gpt4 key购买 nike

n = int(input())
for i in range(1,n+1):
print(" "*(n-i),"#"*i)

我尝试打印:

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

我的解决方案是打印以下结果:

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

在每行之前添加一个额外的空格,因此模式向右移动一个“”。

如何使用 print 并且不在参数之间添加空格?

最佳答案

print 在每个参数之间插入一个,添加sep='':

n = int(input())
for i in range(1,n+1):
print(" "*(n-i),"#"*i, sep="")

或者使用format-strings:

for i in range(1,n+1):
print("{:>{}s}".format("#"*i, n))

关于python - 如何使用 print 并且不在参数之间添加空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53051354/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com