0

Variables

You’re making a contact management system. The given program declares two variables, name and age. Complete the code to output "name is age years old", where name and age are the declared variable values. Guys, can someone solve this for me i’ve tried so many ways but still didn’t work not just using with + that doesn’t leaves spaces so any suggestions.

9th May 2021, 11:49 PM
Sara_889
Sara_889 - avatar
14 Answers
+ 2
Best method is to use f-string: print(f"{name} is {age} years old") Prefix the string with an f and put the variables you want to use in curly braces. No casting needed
10th May 2021, 12:43 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
# in python name = input() age = input() print(f"{name} is {age} years old.")
11th May 2021, 7:01 PM
Asilbek
Asilbek - avatar
+ 1
Ah yes, as I thought. Your approach is not quite right. As I have mentioned before that you can't concatenate string with integer without converting the integer to string type first, so basically you can just try adding str() to the integer variable. Like so : print(name + " is " + str(age) + " years old.") Hope this helps. Also watch out for the proper punctuation in the output that the question asks for.
10th May 2021, 12:09 AM
Dama Dhananjaya Daliman
Dama Dhananjaya Daliman - avatar
+ 1
In Python... nm="Someone" age=16 print("{} is {} years old".format(nm, age)) Hope it helped...!!
10th May 2021, 9:07 AM
User Unknown
User Unknown - avatar
0
If you tried, then show us what have you done so far
9th May 2021, 11:55 PM
Michal Doruch
0
Can you post your attempt so I can figure out what methods you've tried? But maybe I can give you a small advice now, to concatenate string with integer you need to change the integer to string. You can achieve that by using the str() method that converts objects to string type.
10th May 2021, 12:00 AM
Dama Dhananjaya Daliman
Dama Dhananjaya Daliman - avatar
0
Print ( name + “is” + age + “years old”) this is one of my attempts i forget the others
10th May 2021, 12:03 AM
Sara_889
Sara_889 - avatar
10th May 2021, 12:05 AM
Sara_889
Sara_889 - avatar
0
Sara_889 we cannot see the task, if we do not own pro version. Anyway, try str() method mentioned by Dama Dhananjaya Daliman to convert ints to strings: print(str(age)... ) You will learn typecasting in the next lessons, continue learning if you want more specific explanation
10th May 2021, 12:09 AM
Michal Doruch
0
I did but it doesn’t leave space
10th May 2021, 12:10 AM
Sara_889
Sara_889 - avatar
10th May 2021, 12:13 AM
Sara_889
Sara_889 - avatar
0
Sara_889 if you want spaces in between the words just add them manually like if you do: print(name+"is"+str(age)) >> Jamesis42 Instead do : print(name + " is "+str(age)) >> James is 42 Hope you can see the difference.
10th May 2021, 12:19 AM
Dama Dhananjaya Daliman
Dama Dhananjaya Daliman - avatar
0
print(f"{'James'} is {42} years old")
1st Jun 2021, 11:00 PM
David Martinez
David Martinez - avatar
0
name = "James" age = "42" print(f"{'James'} is {42} years old")
25th Oct 2022, 11:36 AM
k.sumathi sweety
k.sumathi sweety - avatar