Skip to content
Advertisement

Which way is better to concatenate strings in python? [closed]

JavaScript

Which way is better in this case and why?

  1. emails.append(user + “@” + domain)
  2. 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

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.

Advertisement