› Forums › Python › From String Concatenation to f-Strings: Understanding {} and Function vs Text in Python
- This topic is empty.
-
AuthorPosts
-
March 20, 2026 at 11:48 pm #6256
Β πΉ Introduction
When learning Python, youβll often see code like:
print("convert_to_km(" + str(N) + ") took " + str(dt))and later:
print(f"convert_to_km({N}) took {dt} seconds ({1/dt}/sec)")To truly understand the second (modern) version, you must first understand the first (basic) one.
This post builds your understanding step-by-step.
πΉ Part 1: String Concatenation (The Foundation)
β The Code
print("convert_to_km(" + str(N) + ") took " + str(dt))
πΉ What is happening?
You are joining multiple pieces of text into one string using
+.
πΉ Break it down
Part Meaning "convert_to_km("plain text str(N)convert number β text ") took "plain text str(dt)convert number β text
πΉ Example
N = 100 dt = 0.002Becomes:
"convert_to_km(" + "100" + ") took " + "0.002"π Final output:
convert_to_km(100) took 0.002
πΉ Why
str()is required"hello " + 5 β ERRORπ Python does NOT mix string + number
β Fix:
"hello " + str(5)
πΉ Key Idea
π
+joins strings
πstr()converts values to strings
πΉ Part 2: Enter f-Strings (Modern Approach)
β The Code
print(f"convert_to_km({N}) took {dt} seconds")
πΉ What changed?
Instead of manually joining:
- Python automatically inserts values using
{}
πΉ Meaning of
{}π
{}= βevaluate this and insert the resultβ
πΉ Example
N = 100 print(f"Value is {N}")π Output:
Value is 100
πΉ You can even calculate
dt = 0.002 print(f"{1/dt} per second")π Output:
500 per second
πΉ Part 3: Why
{N}and not justN?Without
{}:print("convert_to_km(N)")π Output:
convert_to_km(N)β Just text
With
{}:print(f"convert_to_km({N})")π Output:
convert_to_km(100)β Value inserted
πΉ Part 4: Is
convert_to_kma Function Here?Look carefully:
print(f"convert_to_km({N})")π Important:
"convert_to_km("β text{N}β evaluated")"β text
β The function is NOT being called here
πΉ When is the function actually called?
km = convert_to_km(N)π This executes the function
πΉ Comparison
Code What happens convert_to_km(N)β function runs "convert_to_km(N)"β just text f"convert_to_km({N})"β text + value
πΉ Part 5: Can f-Strings Call Functions?
Yes:
print(f"{convert_to_km(N)}")π This:
- Executes function
- Inserts result
πΉ Part 6: Understanding the Full Performance Line
print(f"convert_to_km({N}) took {dt} seconds ({1/dt}/sec)")
πΉ Breakdown
Part Meaning convert_to_km({N})shows input {dt}time taken {1/dt}speed (per second)
πΉ Example Output
convert_to_km(100) took 0.002 seconds (500/sec)
πΉ Final Mental Model
π Inside quotes β text
π Inside{}β Python evaluates
π Outside quotes β real execution
πΉ Conclusion
Youβve now learned the progression:
- String Concatenation
+joins stringsstr()converts values
- f-Strings
{}inserts values automatically- Cleaner and more powerful
- Critical Distinction
- Writing a function name β calling it
πΉ One-Line Summary
π f-strings = clean way to combine text + variables + expressions
- Python automatically inserts values using
-
AuthorPosts
- You must be logged in to reply to this topic.
