How do I produce "John is 20 years old" as output given the variables name="John" age="20"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do I produce "John is 20 years old" as output given the variables name="John" age="20"?

Contact management system

21st Jan 2022, 1:00 PM
Abraham Zimba
Abraham Zimba - avatar
14 Answers
+ 5
name="John" age="20" print(name + 'is' + age 'years old') #myattempt
21st Jan 2022, 1:08 PM
Abraham Zimba
Abraham Zimba - avatar
+ 4
ㅤㅤㅤ i don‘t see why age should here be an integer variable (maybe will be it tested in the SL challange) because only for printing this is in my opinion not needed, and you have nothing here to calculate :)
21st Jan 2022, 3:59 PM
JaScript
JaScript - avatar
+ 4
Use format string formate Example code name = "John" age = 20 print(f"{name} is {age} year old")
22nd Jan 2022, 3:57 PM
Sam 🌟
Sam 🌟 - avatar
+ 3
This is already good, only you need to add one + after age and inputs as follows: name=input() #"John" age=input() #"20" print(name + ' is ' + age + ' years old') #my input here on SL Playground (all inputs at the start) #John #20 # but without # sharp
21st Jan 2022, 1:21 PM
JaScript
JaScript - avatar
+ 3
Thanks everyone name, age = input(), int(input()) print(f'{name} is {age} years old') went through
21st Jan 2022, 4:30 PM
Abraham Zimba
Abraham Zimba - avatar
+ 2
JaScript ㅤㅤㅤ Shadoff 🙏🙏🙏🙏🙏☺️😊
21st Jan 2022, 4:32 PM
Abraham Zimba
Abraham Zimba - avatar
+ 1
name, age = input(), int(input()) print(f'{name} is {age} years old')
21st Jan 2022, 2:25 PM
Shadoff
Shadoff - avatar
+ 1
Hello, Abraham Zimba. I think this should be solved the way many people are suggesting in the comments. The solution should be as follows: name = input () age = input() print(name + ' is ' + age + ' years old') I think that should be enough. Have you tried it? Remember that "+" can be used to concatenate strings. Also, note that i wrote ' is ' instead of 'is', and ' years old' instead of 'years old' (note the empty spaces). I guess that if the code inside of "print" was 'is' and 'years old' instead, the output would be 'Johnis22years old", which is not desirable (no spaces between most of the words) XOXO Nicolás
22nd Jan 2022, 1:22 AM
Nicolas Spisso
Nicolas Spisso - avatar
+ 1
Print(name+"is"+age+"years old")
22nd Jan 2022, 4:11 AM
SK SANOWAR
+ 1
If you are using Python 3.6 or newer, you can use formatted strings like so: print(f'{name} is {age} years old) Just put an F before the string, type it out normally and surround any variables with { }!
22nd Jan 2022, 10:15 AM
Josu Colás
Josu Colás - avatar
0
Please show your attempt first.
21st Jan 2022, 1:05 PM
JaScript
JaScript - avatar
0
print(input(), 'is', int(input()), 'years old') #one-liner But you can refer to @JaScript if you want to take name and age as variables. edit]: eh! there is a error in your code JaScript: take age as int not as string :)
21st Jan 2022, 2:16 PM
NEZ
NEZ - avatar
0
name = "John" age = "20" print(f"{name} is {age} years old")
22nd Jan 2022, 10:30 AM
Kobka Maksym
Kobka Maksym - avatar
0
name = "John" age = "20" print(name + "is" + age + "years old")
23rd Jan 2022, 9:17 AM
Arun Shanmugavel