0

59.2 string formatting (names and ages)

So this is the question that im stuck with trying to use the string formatting method: The .format() method provides an easy way to format strings. Take as input a name and an age, and generate the output "name is age years old". Sample Input James 42 Sample Output James is 42 years old I've solved it using the input method which was learn at the start: name = input() age = input() print(name+ " is "+ age+ ( " years old")) #your code goes here This was the given code and im kinda stuck: name = input() age = int(input()) #your code goes here The code i tried to use to solve it but got stuck name = input() age = int(input()) #your code goes here Msg="{name} is {age} years old". Print(Msg)

19th Apr 2021, 7:53 AM
Zhi Hong
Zhi Hong - avatar
10 Answers
+ 4
Problem: String Formatting The .format() method provides an easy way to format strings. Take as input a name and an age, and generate the output "name is age years old". Sample Input James 42 Sample Output James is 42 years old Solution: name = input() #1. Get name input age = int(input())#2. Get age input a="{} is {} years old".format(name,age) #3. Format string a to include name and age in the places within curly braces print(a) #4. Print the formatted string
3rd Jun 2021, 2:52 AM
Shubhang
+ 4
name = input() age = int(input()) #your code goes here msg = "{0} is {1} years old" .format(name, age) print(msg) #Solution related to the syllabus
27th Jun 2021, 12:32 PM
Mohamed Ikram Ifham
Mohamed Ikram Ifham - avatar
+ 2
Not the best way but you can do this using c style: name="sad" age =12 Msg="Hello my name is %s and I am %d years old"% (name,age) print(Msg) There are also other ways to do this: https://code.sololearn.com/cA13a19A20A7
19th Apr 2021, 8:26 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
a = 7 print(f'{a}')
19th Apr 2021, 8:01 AM
Flash
+ 1
name = input() age = int(input()) msg = "{0} is {1} years old" print(msg.format(name,age))
22nd Feb 2022, 11:27 AM
Game0ver
Game0ver - avatar
0
name=input() age=input() message=f'{name} is {age} years old' print (message )
19th Apr 2021, 8:58 AM
Amir
Amir - avatar
0
name = input() age = int(input()) #your code goes here x = "{} is {} years old".format(name,age) print(x)
1st Jul 2021, 7:31 AM
Lokendra Gupta
Lokendra Gupta - avatar
0
name = input() age = int(input()) #your code goes here print (name, "is", age, "years old")
15th Dec 2021, 3:29 PM
Matt Webb
0
user_name = input() user_age = int(input()) # places input in string and prints print("{name} is {age} years old.".format(name = user_name, age = user_age)) # Alternatively # appends user_name and user_age to user_list user_list = [] user_list.extend([user_name, user_age]) # places user_list elements in string and prints print("{0} is {1} years old.".format(user_list[0], user_list[1]))
5th Mar 2022, 12:38 PM
Matumbai Binale
Matumbai Binale - avatar
0
name = input() age = int(input()) msg= name+" is "+str(age)+" years old" #jsut converrt it to string print(msg)
19th Jul 2022, 3:09 AM
Md Jahid Hasan
Md Jahid Hasan - avatar