python search replace and join with delimiter I'm brand new and been going through some beginner code problems on checkIO. I finished this one today and am posting because I'm sure there is a much more elegant way to accomplish this, and I think this particular problem has a lot of useful applications. I would like to know what are the 'clean and efficient' ways to do it, or maybe what built-ins I haven't learned yet that I should explore. My version seems pretty ugly. Thanks!

Problem: take a tuple of phrases, anytime the word 'right' appears, replace it with 'left'. Create a new string combining all the phrases separated with a comma.

def left_join(phrases):
    """
        Join strings and replace "right" to "left"
    """
    phrases = list(phrases)
    newstr = ''
    for j, phrase in enumerate(phrases):
        if 'right' in phrase:
            mod = dict(enumerate(phrase))
            for key in mod:
                if mod[key] == 'r' and mod[key+4] == 't':
                    i = key
                    mod[i] = 'l'
                    mod[i+1] = 'e'
                    mod[i+2] = 'f'
                    mod[i+3] = 't'
                    mod[i+4] = ''
            switch = (list(mod.values()))
            phrase = ''.join(switch)
            if j == (len(phrases) - 1) or len(phrases) == 1:
                newstr += phrase
            else:
                newstr += phrase + ','
        else:
            if j == (len(phrases) - 1) or len(phrases) == 1:
                newstr += phrase
            else:
                newstr += phrase + ','
    return newstr

test1 = ("bright aright", "ok", "brightness wright",)
test2 = ("lorem","ipsum","dolor", "fright", "nec","pellentesque","eu",
         "pretium","quis","sem","nulla","consequat","massa","quis",)
Welcome to code review, good job using a docstring and trying to make your code readable.


Space after a comma: 

test2 = ("lorem","ipsum","dolor", "fright", "nec","pellentesque","eu",
"pretium","quis","sem","nulla","consequat","massa","quis",)

According to PEP0008 which I recommend checking for being the official Python style guide: a space should be left after a comma for readability.

Which should look like:

test2 = ('lorem', 'ipsum', ...)
Use descriptive names: names like left_join() newstr j are not describing the objects they represent ex: your function name can be def replace_word(words, word, new_word): 
Type hints: You may use type hints to indicate what are the inputs and outputs in the following way: 

def your_function_name(phrases: tuple) -&gt; str:
Docstrings: Good job using a docstring for your function, however the description is not very clear here's a better description and btw docstrings should indicate what the input and the output is (what the function parameters are and what it returns):

def your_function_name(phrases: tuple, old_word: str, new_word: str) -&gt; str:
    """
    Replace the old_word by the new_word if found in phrases.
    Args:
        phrases: Tuple of words.
        old_word: The word to replace.
        new_word: The replacement.
 Return:
     Joined string after replacements.
    """

str.replace() and str.join() You can use the replace method to achieve the same results in a shorter code and since you're familiar with join():

Instead of doing this for every letter in every word:

else:
    newstr += phrase + ','


You can do:

', '.join(words)


Code might look like:

And Note: You usually wouldn't be creating a function for such a simple task but use replace() and join() directly, I'm creating a function just for the sake of the example.

def replace_word(phrases: tuple, old_word: str, new_word: str) -&gt; str:
    """
    Replace the old_word by the new_word if found in phrases.
    Args:
        phrases: Tuple of words.
        old_word: The word to replace.
        new_word: The replacement.
 Return:
     Joined string after replacements.
    """
    return ', '.join(word.replace(old_word, new_word) for word in phrases)


if __name__ == '__main__':           
    test1 = ("bright aright", "ok", "brightness wright",)
    test2 = ("lorem","ipsum","dolor", "fright", "nec","pellentesque","eu",
 "pretium","quis","sem","nulla","consequat","massa","quis",)
    print(replace_word(test1, 'right', 'left'))
    print(replace_word(test2, 'right', 'left'))


Output:


  bleft aleft, ok, bleftness wleft
  
  lorem, ipsum, dolor, fleft, nec, pellentesque, eu, pretium, quis, sem, nulla, 
  consequat, massa, quis