-1

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?

9
  • 2
    Seems like f-strings are the simplest and cleanest way to do it. Commented Feb 11, 2023 at 2:13
  • 1
    I think concatenating strings with plus is the normal practice. Commented Feb 11, 2023 at 2:15
  • 1
    Yes, f-strings are the third example. Commented Feb 11, 2023 at 2:15
  • 1
    3, if your python version supports f-strings, otherwise, 1. Commented Feb 11, 2023 at 2:15
  • 2
    Way back in ancient history, like Python 1, + was slow and it was important to use join. 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. Commented Feb 11, 2023 at 2:19

1 Answer 1

3

Short answer

You should use f-strings (third way).

f-strings are more readable, concise, and less error-prone than other means. In my test (test speed). Using f-strings is the fastest way.

Long answer

f-strings are a great way to format strings. Not only are they more readable, concise, and less error-prone than the alternatives, they're also faster!

Simple Syntax

The syntax is similar to what you would do with str.format(), but less verbose. Of the four ways, it's the easiest to read, right?

Using an uppercase letter for f (F) is also possible:

query_category = F"Query: {query} Category: {category}"

Arbitrary Expressions

Because f-strings are evaluated at runtime, you can put any and all valid Python expressions into them. This allows you to do some nifty things.

You can do something very simple like this:

nine_times_nine = f"{9 * 9}"

Functions can also be called:

def function():
    …

call_function = f"{function()}"

Summary

f-strings are the perfect way to use them. For more information, check out this article.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.