Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

print a string from an input but also add the number to the end of the string

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test1, test2, test3, test4

Any help is greatly appreciated, am very new to python. Thanks!

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading