AppDividend
Latest Code Tutorials

Python String Join Example | Python Join Method Tutorial

0

Python String Join Example | String.join() Method Tutorial is today’s topic. Python join() method is the method which returns the string concatenated with the elements of an iterable. Python String join() method provides a flexible way to concatenate a string. It concatenates each element of an iterable (such as a list, string, and a tuple) to the string and returns the concatenated string.

Python String Join Example

Python String join() method is the string method which returns the string in which the elements of a sequence have been joined by string separator.

The syntax of Python String join() method is following.

string.join(iterable)

Python String join() method takes required iterable which are objects capable of returning its members one at a time. Some examples are List, Tuple, String, Dictionary, and Set

If the iterable contains any non-string values, it raises a TypeError exception.

See the following example. Let’s define a tuple and then join that tuple values with the and see the output.

# app.py

venom = ('Knull', 'Toxin', "Carnage")
single = '|'.join(venom)
print(single)

See the output.

 

Python String Join Example

Join Method in List in Python

Let’s define a list and then join the items with the join method with a separator.

# app.py

DC = ['WonderWoman', 'Aquaman', 'Batman', 'Superman']
movies = '||'.join(DC)
print(movies)

Okay, so we have defined a list and then join with a separator || and now see the output.

 

Join Method in List in Python

Join Method in Dictionary in Python

Let’s define a dictionary and then join the items with the join method with a separator.

# app.py

trends = {
    1: 'AI',
    2: 'Machine Learning',
    3: 'Serverless',
    4: 'ARVR'
}
picks = '/'.join(trends.values())
print(picks)

Here, we have used the dictionary values and join them with a separator /. You can see the output.

 

Join Method in Dictionary in Python

Finally, Python String Join Example | Python Join Method Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.