Python String join()

Filed Under: Python

Python string join() function is used to concatenate a sequence of strings to create a new string.

Python String join()

The basic syntax of Python join() function is like the below image.

Python Join Syntax, python string join

Here str1 will be used as the separator between the concatenating elements of the sequenceOfString. Now, the following example will help you to visualize things.


# initialize the list of string
str_list = ['My', 'favourite', 'fruit', 'is', 'banana']
# initialize the separator
separator = '-'

# use python string joint to join the words of the string
output = separator.join(str_list)

# print the output result
print(output)

You will get the output like the following.


My-favourite-fruit-is-banana

Using A Single String Python Join Example

In the previous section, we used a list of string to join together. Now, what will happen if we use a single string instead of a list to join. Let’s see the next example. In this example, we will use space as separator. So the code will be like below.


# initialize the single string
alphabets = 'abcdefghijklmnopqrstuvwxyz'

# initialize the separator
sep = ' '

# use python string joint to join that single string
output = sep.join(alphabets)

# print the output result
print(output)

So the output will be


a b c d e f g h i j k l m n o p q r s t u v w x y z

Converting Python Join to a raw function

You should know that you can make your own function that can also do the same task that Python string join can do. In this section, we are going to see how to write that function.

If you look closely, you will see that the separator is appended after each item of the list except the last one. So we will write a function in which we will append the separator after each item.

After that, we will append the last item of the list to the result string. The following example will illustrate the idea.


def list_join(string_list, sep):
    result = ''
    for i in range(0, len(string_list)-1):
        result = result+string_list[i]+sep

    result = result+string_list[len(string_list)-1]
    return result

# initialize the single string
alphabets = 'abcdefghijklmnopqrstuvwxyz'
# initialize the separator
sep = ' '

# use python string joint to join that single string
output = list_join(alphabets, sep)

# print the output result
print(output)

And you will get same output same as the output you got before with python join function.


a b c d e f g h i j k l m n o p q r s t u v w x y z

So, that’s all about python join function. Hope that you understand the topic clearly. If you have any query, feel free to comment below. Happy Learning.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Comments

  1. photoshop online says:

    I visit your blog regularly to get new things like this. Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages