Strings in Python are immutable, and so 'string a' + 'string b' has to make a third string to combine them. Say you want to clone a string, by adding each item to the string will get \ time, as opposed to \ as you would get if it were a list.

And so, the best way to join an iterable by a separator is to use str.join.

>>> ','.join('abcdef')
'a,b,c,d,e,f'

If you want to do this manually, then I'd accept the \ performance, and write something easy to understand. One way to do this is to take the first item, and add a separator and an item every time after, such as:

def join(iterator, seperator):
    it = map(str, iterator)
    seperator = str(seperator)
    string = next(it, '')
    for s in it:
        string += seperator + s
    return string
Answer from Peilonrayz on Stack Exchange
🌐
GeeksforGeeks
geeksforgeeks.org › python › join-list-with-separator-in-python
Join List With Separator in Python - GeeksforGeeks
July 23, 2025 - For example, given the list a = ... resulting in a string like this: "GFG,Java,DSA". str.join() is the most efficient way to join elements of a list with a separator ....
Top answer
1 of 4
29

Strings in Python are immutable, and so 'string a' + 'string b' has to make a third string to combine them. Say you want to clone a string, by adding each item to the string will get \ time, as opposed to \ as you would get if it were a list.

And so, the best way to join an iterable by a separator is to use str.join.

>>> ','.join('abcdef')
'a,b,c,d,e,f'

If you want to do this manually, then I'd accept the \ performance, and write something easy to understand. One way to do this is to take the first item, and add a separator and an item every time after, such as:

def join(iterator, seperator):
    it = map(str, iterator)
    seperator = str(seperator)
    string = next(it, '')
    for s in it:
        string += seperator + s
    return string
2 of 4
5

Let's take that step by step:

def join(l, sep):
    out_str = ''
    for i, el in enumerate(l):

Here, why do you need the enumerate? You could write for el in l:

        out_str += '{}{}'.format(el, sep)

.format is not super efficient, there are other methods. You can have a look at This question for some researches and benchmarks on performances.

    return out_str[:-len(sep)]

This makes little sense for l = [] if len(sep) > 1. ''[:-1] is valid, and returns '', because python is nice, but it is not a very good way of getting around that limit case.

In general, adding something just to remove it at the end is not great.

Creating an iter, looking at the first value, then adding the rest, as it has been suggested in other answers, is much better.

I would also recommend writing some unit tests, so that you can then play around with the implementation, and stay confident that what you write still works.

Typically, you could write:

# Empty list
join([], '') == ''
# Only one element, -> no separator in output
join(['a'], '-') == 'a'
# Empty separator
join(['a', 'b'], '') == 'ab'
# "Normal" case
join(['a', 'b'], '--') == 'a--b'
# ints
join([1, 2], 0) == '102'
Discussions

python - How can I use str.join() instead of "+=" to join a list of strings with separator? - Stack Overflow
I have some code which is essentially this: data = ["some", "data", "lots", "of", "strings"] separator = "." output_string = ""... More on stackoverflow.com
🌐 stackoverflow.com
python - How to flatten a list of lists and join with a separator - Stack Overflow
@Lee +1 for the reduce - that's ... is using Python 3.x, then they'll need a from functools import reduce)... I've also proposed a generator solution which I think is a bit cleaner using some itertools stuff, but reduce is easily the best way here :) 2014-06-09T10:50:57.57Z+00:00 ... You can tee the list as an iterable and only yield the separator when there's a following item. Here we're defining a function called joinlist which contains ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to concatenate (join) items in a list to a single string - Stack Overflow
For handling a few strings in separate variables, see How do I append one string to another in Python?. For the opposite process - creating a list from a string - see How do I split a string into a list of characters? or How do I split a string into a list of words? as appropriate. ... Save this answer. ... Show activity on this post. ... Use with AICopy>>> words = ['this', 'is', 'a', 'sentence'] >>> '-'.join... More on stackoverflow.com
🌐 stackoverflow.com
python - join with a separator added at the end when not empty - Stack Overflow
If join added a separator after each element, it would just this: string += '.' + '.'.join(args[f(n) : f(n+1)]) + '.' + suffix ... But islice may be even worse since it iterates through the start of the list: itertools.islice compared to list slice · Some alternatives are discussed here: Avoiding unnecessary slice copying in Python... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › TheAlgorithms › Python › blob › master › strings › join.py
Python/strings/join.py at master · TheAlgorithms/Python · GitHub
Program to join a list of strings with a separator · """ · · def join(separator: str, separated: list[str]) -> str: """ Joins a list of strings using a separator · and returns the result. · :param separator: Separator to be used · for joining the strings.
🌐
Tutorial Gateway
tutorialgateway.org › python-string-join
Python string join
June 18, 2019 - The Python string join method is used to join or concatenate a string with an iterable. This function accepts an iterable as an argument. It joins a given separator while concatenating the strings in an iterable.
🌐
Programiz
programiz.com › python-programming › methods › string › join
Python String join() | Programiz
March 14, 2024 - The join() method tries to join the keys (not values) of the dictionary with the string separator.
🌐
Mimo
mimo.org › glossary › python › string-join-method
Python's String Join Method: Efficient String Concatenation
Join with a space separator separator = ' ' sentence = separator.join(words) print(sentence) # Outputs: 'Hello world this is Python' # 2.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-join-method
Python String join() Method - GeeksforGeeks
join() method in Python is used to combine elements of an iterable into a single string. It places the specified separator between each element. It is commonly used with lists and tuples, but it can work with any iterable whose elements are strings.
Published: October 1, 2026
🌐
Real Python
realpython.com › python-join-string
How to Join Strings in Python – Real Python
October 22, 2025 - Python’s built-in string method .join() lets you combine string elements from an iterable into a single string, using a separator that you specify. You call .join() on the separator, passing the iterable of strings to join.
🌐
W3Schools
w3schools.com › python › ref_string_join.asp
Python String join() Method
Python Examples Python Compiler ... method takes all items in an iterable and joins them into one string. A string must be specified as the separator....
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python string join() method
String Join() Method in Python
January 10, 2025 - words = ["Python", "is", "fun"] result = " ".join(words) print(result) # Output: "Python is fun" Here, the " " (space) acts as the separator between the words. To explore more about lists and the operations you can perform with them, read Python ...
🌐
AskPython
askpython.com › python › string › python-string-join-method
Python String join() function - AskPython
September 29, 2026 - It takes an iterable of strings and glues them together with the string it was called on sitting between each pair. The syntax puts the separator first, which means the receiver and the argument play opposite roles from what most beginners guess.
🌐
freeCodeCamp
freecodecamp.org › news › python-join-how-to-combine-a-list-into-a-string-in-python
Python join() – How to Combine a List into a String in Python
January 3, 2023 - join() is a built-in string function so it requires a string to invoke it · it returns one output value, a string formed by combining the elements in the iterable with the string invoking it, acting as a separator
🌐
Medium
ogre51.medium.com › python-string-join-split-5d5beca17b0c
Python: STRING .join() & .split() | by Rahul S | Medium
September 29, 2023 - The .join() method is used to concatenate elements from an iterable (e.g., a list, tuple, or string) into a single string, using the calling string as the separator. Syntax: "separator".join(iterable) Example: # Joining a list of words into ...
🌐
Intellipaat
intellipaat.com › home › blog › python join: functions and methods explained
Python Join: Functions and Methods Explained - Intellipaat
September 8, 2026 - The join() function is called on the separator string, not the iterable. It combines all string elements from the iterable using that separator. ... Python Pro: Start Your Coding Journey Today!