I want to insert some strings in a string.
All I know is that there are four ways to do this, here are four examples:
query = "What type of code should I use to insert some strings in a string?"
category = "Python"
query_category = "".join(["Query: ", query, " Category: ", category])
or
query = "What type of code should I use to insert some strings in a string?"
category = "Python"
query_category = "Query: " + query + " Category: " + category
or
query = "What type of code should I use to insert some strings in a string?"
category = "Python"
query_category = f"Query: {query} Category: {category}"
or
query = "What type of code should I use to insert some strings in a string?"
category = "Python"
query_category = "Query: {query} Category: {category}".format(query = query, category = category)
What type of code should I use to insert some strings in a string? Can anyone explain the pros and cons of each code?
+was slow and it was important to usejoin. The difference is no longer very important, except in special cases. You are guilty of premature optimization here, wasting time on relatively unimportant issues. First, make it work in a way that makes sense to you. THEN decide if it is fast enough.