如何根据用户输入的内容打印出一定数量的字母?

时间:2019-07-31 23:47:21

标签: python python-3.x

类似这样

Underscores = int(input("Enter a number: "))
print("^" + Underscores + "^")

那是我试图做的,但是没有用,您可能会从代码中了解我正在尝试做的事情。基本上,用户在“ ^ ^”表情符号中输入了他们想要的“ ”个数,例如输入为4时,它会打印出“ ^ ____ ^”。如果输入为10,则输出“ ^ __________ ^”等,谢谢!

到目前为止,我只尝试过此代码,并在线查找其他来源

1 个答案:

答案 0 :(得分:2)

您很近。您可以在python中将字符串相乘,这样:

'_' * 4

给您

'____'

这样,您只需要对代码做些改动:

n = int(input("Enter a number: "))
print("^" + '_' * n  + "^")

# or maybe cleaner:
print("^{}^".format("_" * n))