Python Forum

Full Version: How to print string multiple times on new line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I get the below to print the output 4 x and each print to start on a new line

name = input("What is your name: ")

age = input("What is your age: ")

result = str(2020 + (100 - int(age)))

print(4 * ("Hello " + name +" you will be 100yrs old in the year " + result + ". "))
Thanks
Call print 4 times?
print(*4*(("Hello " + name +" you will be 100yrs old in the year " + result + ". "), sep='("Hello " + name +" you will be 100yrs old in the year " + result + ". "')
(Sep-30-2020, 12:54 PM)Naheed Wrote: [ -> ]print(*4*(("Hello " + name +" you will be 100yrs old in the year " + result + ". "), sep='("Hello " + name +" you will be 100yrs old in the year " + result + ". "')

Seriously? For start - it will raise SyntaxError...
(Sep-30-2020, 01:00 PM)buran Wrote: [ -> ]Seriously? For start - it will raise SyntaxError...
Please, put forward your suggestions so I can find the key areas for improvement.
(Sep-30-2020, 01:13 PM)Naheed Wrote: [ -> ]put forward your suggestions so I can find the key areas for improvement
first of all - try to fix the SyntaxError in your code
then read on loops
In spoken language: "print string four times separated with newline"

Translation into Python:

name = 'Guido'
age = '64'
message = f'Hello {name}! You will be 100 years old in {2020 + (100 - int(age))}'
print('\n'.join(message for i in range(4)))
for _ in range(4):
    print(f"Hello {name}. You will be 100yrs old in the year {result}.")