+ 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

11th Jan 2023, 2:50 AM
Jessie
Jessie - avatar
3 ответов
+ 9
Jessie , This can be done using string concatenation also, https://code.sololearn.com/ck0D5G9VMjm2/?ref=app
11th Jan 2023, 4:56 AM
Riya
Riya - avatar
+ 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}")
11th Jan 2023, 3:03 AM
Mozzy
Mozzy - avatar
+ 3
welcome = lambda :print(f"welcome {input()}!") welcome()
11th Jan 2023, 6:05 AM
Bob_Li
Bob_Li - avatar