Write a program to take x and y as input and output the string x, repeated y times. Sample Input hi 3 Sample Output hih
18 Answers
New Answer18 Answers
New AnswerAdi Nath Bhawani Your for loop is unnecessary. You can just multiply string by int: x*y = 'hi'*3 = 'hihihi' :)
Hello :3 Python has functionality to print string multiple times so if you want to print hi 3 times then just do this: print ('hi' * 3) Since there are many test cases so you have to take input then implement like this: print (input() * 3)
x = input() y = int(input()) print((x*y)[:y]) Use (x*y)[:y] for printing 'hih' else only (x*y)
Shouldn't it be 'hihihi'? The code is fairly simple. You take an input twice. The second time convert it to int, then print the product so first input × second input.
Hello :3, Due to question you have 2 different input,bone should be string, named x, another is integer, named y. x = input() # input is already string as default y = int(input()) # now sure it is integer So the result is print(x * y) # will give you y times string x
print(input ()*3) It will automatically typecast in string because the default is string in input
# Run in Ruby editor def code(x,y) p (x * y)[0...y] # multipy string x, y times # [0...y] is a range to select the string chars from 0 upto y end code "hi",3 # "hih"
print(input()*int(input)) That will do the work perfectly The first input gets the string and the second inputs gets the number of times that the string is repeated
So the sample output would be hi 3 times: “hihihi” So to break it down, first you need to store the user inputs in the variables x and y: x = input() y = input() Since y is an int you need to convert it from the string input: y = int(y) If you want to store your output in a variable you can so: output = x * y Then print the output: print(output) Hopefuly breaking it down helps you see it better. You can write it more concisely like other answers like so: print(input()*int(input()))
x=input() y=int(input()) z="" for i in range(1,y+1): z=z+x print(z) If it has to print hihihi
If it has to print "hih" only, then try this x=input() y=int(input()) z="" for i in range(1,y+1): z=z+x print(z[0:y])
Guys we don't need to print "hi" three time. This is just sample text, I don't know what to fill in input 😕
Then try which I gave with respect to hih then see if it works or not. I hope it works 🤞
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message