JavaScript
x
7
1
def email_list(domains):
2
emails = []
3
for domain, users in domains.items():
4
for user in users:
5
emails.append(user + "@" + domain) # emails.append("{}@{}".format(user, domain))
6
return(emails)
7
Which way is better in this case and why?
- emails.append(user + “@” + domain)
- emails.append(“{}@{}”.format(user, domain))
Advertisement
Answer
In my honest opinion, f-strings are the best. They are flexible and readable. Unlike other concats, you don’t have to make sure you insert all strings. Infact, you can do something like that:
JavaScript
1
4
1
number = 9
2
text = "hello"
3
print(f"{text} {number}")
4
Also, it will automatically convert eventual quotes and special characters!
Globally, there isn’t a solution better than another. It’s all depending on the use case. But generally, fstrings are better.