如何在python中的for循环中连接字符串?

时间:2019-03-04 03:27:56

标签: python string for-loop concatenation string-concatenation

我的任务是产生一个用户输入编号为“ V”的“波形”。

示例:

How many Waves you want to see?
User Input: 5

输出:

vvvvv

以下是我的代码:

print("This is a program for generating waves as per the user")
#This program displays waves as per the user

counT=int(input("How many waves would you like to see: "))
# Get User's number of waves

for wavE in range(1,counT+1):
    wavE='v'
    print(wavE)

我的输出为:

v
v
v
v
v

任何人都可以帮助我获得所需的输出:

vvvvv

谢谢。

1 个答案:

答案 0 :(得分:0)

使用它作为完整代码:

print("This is a program for generating waves as per the user")
counT=int(input("How many waves would you like to see: "))
print('v'*counT)

输出:

This is a program for generating waves as per the user
How many waves would you like to see: 5
vvvvv

或者如果您需要使用循环:

print("This is a program for generating waves as per the user")
counT=int(input("How many waves would you like to see: "))
s = ''
for i in range(counT):
    s += 'v'