Reputation: 1
I need to insert firstnoun
into the sentence "The [firstnoun] went to the lake.", where firstnoun
is user inputted.
This is sort of what I have so far:
firstnoun = input("Enter your first noun here: ")
I need it to print:
The [firstnoun] went to the lake.
How do I do this? I tried doing
print("The" (print(firstnoun)) "went to the lake.")
and variations thereof, but none of that is working. I hope the question is clear enough.
Upvotes: 0
Views: 6984
Reputation: 1220
Looking at the python docs, you can find multiple ways.
firstnoun = input("Enter your first noun here:")
print("The " + firstnoun + " went to the lake")
print("The %s went to the lake" % firstnoun)
print("The {} went to the lake".format(firstnoun))
or even using format
with keywords
print("The {noun} went to the lake".format(noun=firstnoun))
Upvotes: 4
Reputation: 15356
You need to interpolate the value.
Here's a general example to show the concept which you can then apply to your homework:
x = "foo"
print("The word is {0}".format(x))
Also, no, a main
function is not necessary.
Upvotes: 0
Reputation: 236140
Use string concatenation to build the output you wish:
print("The " + firstnoun + " went to the lake.")
For more advanced formatting, use format()
:
print("The {0} went to the lake.".format(firstnoun))
Upvotes: 1