I would like to be able to input a number, then word and have the output reflect the word number. So far I can get it to print the word but am lost as to how to add the number to the end.
times = input('How many?')
word = input('Enter your word:')
print(''.join([word] * int(times)))
So if I input
How many? 4
Enter your word: test
The output would be
test1, test2, test3, test4
Any help is greatly appreciated, am very new to python. Thanks!
CodePudding user response:
Using f-strings and str.join
:
>>> times = 4
>>> word = "test"
>>> ", ".join(f"{word}{n}" for n in range(1, times 1))
'test1, test2, test3, test4'
CodePudding user response:
in Python there is a thing called f-strings, they automatically format your string. So you can rewrite it as this:
times = int(input("How many? "))
word = input("Enter your word: ")
printStr = ""
for i in range(times):
printStr = f"{word}{i 1}, "
printStr = printStr[:-2] # This cuts off the last comma and space
print(printStr)