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)
10 Respuestas
+ 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
+ 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
+ 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
+ 1
a = 7
print(f'{a}')
+ 1
name = input()
age = int(input())
msg = "{0} is {1} years old"
print(msg.format(name,age))
0
name=input()
age=input()
message=f'{name} is {age} years old'
print (message )
0
name = input()
age = int(input())
#your code goes here
x = "{} is {} years old".format(name,age)
print(x)
0
name = input()
age = int(input())
#your code goes here
print (name, "is", age, "years old")
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]))
0
name = input()
age = int(input())
msg= name+" is "+str(age)+" years old" #jsut converrt it to string
print(msg)