Python Forum
How to print string multiple times on new line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print string multiple times on new line
#1
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
Reply
#2
Call print 4 times?
Reply
#3
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 + ". "')
Reply
#4
(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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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)))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
for _ in range(4):
    print(f"Hello {name}. You will be 100yrs old in the year {result}.")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 335 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  splitting file into multiple files by searching for string AlphaInc 2 814 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,391 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,575 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Print the line before the corrent line tester_V 9 1,488 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  Remove a space between a string and variable in print sie 5 1,706 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,144 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Matching multiple parts in string fozz 31 6,056 Jun-13-2022, 09:38 AM
Last Post: fozz
  Can you print a string variable to printer hammer 2 1,891 Apr-30-2022, 11:48 PM
Last Post: hammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020