In Python, situations where you want to combine multiple strings stored in a list into a single string occur frequently. For example, you might want to join them with commas to create CSV data, or connect words with spaces to form a sentence.
For such processing, use the string method join(). While you can use the + operator in a loop to combine strings, using join() is overwhelmingly better in terms of performance (processing speed) and makes the code more concise.
This article explains the basic usage of the join() method and what to do when your list contains numbers.
Basic Syntax of the join() Method
The join() method is called on the “separator”, not the list itself. This part might feel slightly unintuitive for beginners.
Syntax:
"separator".join(iterable)
It concatenates the elements of the list passed as an argument, inserting the specified separator between each element.
Specific Usage Examples
1. Concatenating with Spaces or Symbols
Here is an example of connecting a list of words with spaces to make a sentence, or connecting parts with hyphens to create an ID.
# List of words
word_list = ["Python", "is", "very", "useful"]
# Join with a space
sentence = " ".join(word_list)
print(f"Original list: {word_list}")
print(f"Result: {sentence}")
# Join with a hyphen
id_parts = ["user", "001", "tokyo"]
user_id = "-".join(id_parts)
print(f"ID: {user_id}")
Execution Result:
Original list: ['Python', 'is', 'very', 'useful']
Result: Python is very useful
ID: user-001-tokyo
2. Concatenating Without a Separator (Empty String)
If you want to join list elements without any gaps, call join() on an empty string "".
# List of characters
char_list = ["P", "r", "o", "g", "r", "a", "m"]
# Join as is
combined_text = "".join(char_list)
print(f"Combined: {combined_text}")
Execution Result:
Combined: Program
3. Concatenating with Commas (CSV Format)
Here is an example of creating a CSV (comma-separated values) formatted string, which is often used in data analysis.
# Data for one CSV row
csv_row_data = ["2025-12-01", "Item-A", "In Stock"]
# Join with comma
csv_line = ",".join(csv_row_data)
print(f"CSV Line: {csv_line}")
Execution Result:
CSV Line: 2025-12-01,Item-A,In Stock
Important Note: When Numbers are Included (TypeError)
The list passed to the join() method must contain only strings (str). If it includes integers (int) or other types, a TypeError will occur.
# List containing numbers
number_list = [10, 20, 30]
# Example that causes an error
# result = ",".join(number_list)
# TypeError: sequence item 0: expected str instance, int found
Solution: Convert to Strings Using map()
If you want to concatenate a numeric list, use the map() function to convert all elements to str type before passing them to join().
# Numeric list
scores = [98, 75, 80, 100]
# Convert each element to string with map(str, scores) and then join
score_csv = ",".join(map(str, scores))
print(f"Score CSV: {score_csv}")
Execution Result:
Score CSV: 98,75,80,100
You can also use list comprehension [str(x) for x in scores], but map() is generally more concise and memory-efficient for simple conversions.
Summary
- Use
"separator".join(list)to concatenate a list of strings. - It is faster and more efficient than using
+in a loop. - To join without a separator, use
"".join(list). - If the list contains numbers, you must convert them to strings using
map(str, list).
