Concatenation in Python – String Joining Techniques for Data Science 2026
String concatenation is one of the most fundamental operations in data science. Whether you are building SQL queries, constructing log messages, creating feature names, combining text columns, or preparing data for NLP models, knowing the most efficient and Pythonic ways to join strings is essential. In 2026, the modern approaches (especially .join() and f-strings) make concatenation fast, readable, and memory-efficient.
TL;DR — Best Ways to Concatenate Strings
str1 + str2→ simple but slow for many strings"".join(list_of_strings)→ fastest and most Pythonic- f-strings (
f"{var1}{var2}") → cleanest for small numbers of variables .format()→ good for complex templates
1. Basic Concatenation Methods
first = "Data"
second = "Science"
# 1. + operator (simple but not ideal for many strings)
result = first + " " + second
# 2. .join() - the most efficient way
words = ["Python", "is", "great", "for", "data", "science"]
sentence = " ".join(words)
print(sentence)
# 3. f-strings (modern and very readable)
name = "Alice"
age = 28
message = f"User {name} is {age} years old."
2. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("customer_data.csv")
# Example 1: Create full name column
df["full_name"] = df["first_name"] + " " + df["last_name"]
# Example 2: Build feature names dynamically
prefix = "feature_"
features = ["amount", "quantity", "profit"]
full_features = [prefix + f for f in features]
# Example 3: Construct SQL query safely
columns = ["customer_id", "order_date", "amount"]
query = "SELECT " + ", ".join(columns) + " FROM sales WHERE amount > 1000"
3. Performance Comparison (Important for Large Data)
# Avoid repeated + in loops (creates many temporary strings)
bad = ""
for word in words:
bad += word + " " # inefficient
# Best way
good = " ".join(words) # only one string created
4. Best Practices in 2026
- Use
.join()whenever you have a list or iterable of strings - Use f-strings for small numbers of variables (very readable)
- Use pandas
.str.cat()for concatenating columns in DataFrames - Avoid
+inside loops — it is quadratic in time - Use
.format()for complex templates or when you need reuse
Conclusion
String concatenation is a basic but extremely important skill before diving deeper into Regular Expressions. In 2026 data science projects, prefer .join() for joining lists of strings and f-strings for simple cases. These techniques make your code cleaner, faster, and more memory-efficient when building queries, log messages, feature names, and text features.
Next steps:
- Review your current string-joining code and replace any inefficient
+loops with.join()or f-strings