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
  append a string to a modified line Mr_Blue 10 285 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
Lightbulb Multiple inputs on the same line (beginner) dementshuk 9 319 Sep-03-2021, 02:21 PM
Last Post: dementshuk
  Replace String in multiple text-files [SOLVED] AlphaInc 5 514 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  How to capture string from a line to certain line jerald 1 354 Jun-30-2021, 05:13 PM
Last Post: Larz60+
  If match not found print last line tester_V 2 971 Apr-26-2021, 05:18 AM
Last Post: tester_V
Question How to extract multiple text from a string? chatguy 2 593 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  Multiple Line Chart Plotting moto17 1 923 Jan-20-2021, 01:38 PM
Last Post: wostan
  if a string has a digit - print tester_V 2 596 Jan-16-2021, 04:48 AM
Last Post: tester_V
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 729 Dec-15-2020, 05:21 PM
Last Post: snippsat
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 756 Dec-02-2020, 07:50 AM
Last Post: Gilush

Forum Jump:

User Panel Messages

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