+ 1
Why does this code work?
Original code was def welcome(): print("Welcome, user") welcome() #and I did this below def welcome(): name = input() welc = ("Welcome, user") print(welc.replace("user",name)) welcome() # this worked but why did it work? It just made sense to me this way, but how else would one modify the welcome message in its defined function to print the user's inputted name? I'm curious
3 Answers
+ 9
Jessie ,
This can be done using string concatenation also,
https://code.sololearn.com/ck0D5G9VMjm2/?ref=app
+ 6
Hi Jessie,
One way is by using f-strings to format the message string, instead of replacing a substring.
def welcome():
name = input()
print(f"Welcome, {name}")
+ 3
welcome = lambda :print(f"welcome {input()}!")
welcome()