Introduction to Python String Join
Sometimes, during the data exploratory and preparation phase, we encounter the data in the form of huge iterable lists. What if we need the same in the form of a comma-separated string to make the entire process of data preparation more efficient. Python Programming Language has an inbuilt function named join(), which indeed helps concatenate iterable objects (like list, dict, tuple, string, set, etc.) – which generally returns their elements one at a time.
The join() function is eventually a string-based function that returns a single string by combining the iterable object passed as an argument to it.
Syntax:
str_name.join(iterable_Object)
The syntax is defined as below:
- str_name: It is the string’s name where the concatenated elements of the iterable object will be stored.
- iterable_Object: The join() function takes up an iterable object as its argument.
- Return value: The join() function returns a string that is formed out by joining the elements out of the iterable object passed as an argument to it.
- Type Error: Join() function supports only string values in the iterable objects. If the iterable object contains any non-string value, then the join() function will raise a Type Exception.
Examples of Python String Join
Let’s take an example to understand more about Python String join() function:
Example #1
Code:
# Python program to demonstrate the usage of join() function to concatenate the elements of an iterable object passed as an argument
list = ['Sugar', 'Salt', 'Pepper', 'Pots', 'loves', 'the', 'Iron-Man']
# This seperator is used to seperate each object of the iterable element when concatenated by the join() function
seperator = "-"
# Using join() function to concatenate the elements of list and store the returned string in the variable "Str"
Str = seperator.join(list)
#Prints the string "Str" using the Python Print function
print(Str)
Output:
What if you do not want a seperator to come up in the string returned by the join() function at all & require it to return a sentence line structure with each word/ string seperated by a space. All we need is to alter the seperator as space over here.
Let’s try the same.
Example #2
Code:
# Space as a seperator in join function
list = ['Sugar', 'Salt', 'Pepper', 'Pots', 'loves', 'the', 'Iron-Man']
seperator = " "
Str = seperator.join(list)
print(Str)
Output:
Example #3
Suppose we don’t want any separator at all when we are using the join function to concatenate the elements of an iterable object.
This can be taken care of in two ways:
Case 1: Specify the separator as a blank string and utilize the same with the Python join() function.
Code:
# Using a blank string as a seperator in join function
list = ['S', 'u', 'g', 'a', 'r']
seperator = ""
Str = seperator.join(list)
print(Str)
Output:
Case 2: Instead of declaring the variable explicitly as a blank string. Directly use a blank string out there with the join() function with syntax as “.join(iterable object)
# Using a blank string as a seperator in join function
list = ['S', 'u', 'g', 'a', 'r']
Str = "".join(list)
print(Str)
Output:
What will happen if any non-string value is present as an element of the iterable object? How will the join() function react to that?
Will it convert it into a string on its own and provide us with the expected output, or it will throw an error?
Example #4
Code:
# Python program to demonstrate the usage of join() function with iterable objects having non-string values
list = ['Sugar', 'Salt', 'Pepper', 'Pots', 'loves', 9 , 'Iron-Man']
# This seperator is used to seperate each object of the iterable element when concatenated by the join() function
seperator = "-"
# Using join() function to concatenate the elements of list and store the returned string in the variable "Str"
Str = seperator.join(list)
#Prints the string "Str" using the Python Print function
print(Str)
Here we have a non-string element in the list.
Output:
Conclusion
Yes, Python join() function throws an error if the iterable object contains a non-string value. In short, it works with only string values as a part of the iterable object that is passed to it as an argument.
Recommended Articles
This is a guide to Python String Join. Here we discuss the introduction to Python String Join along with the respective Syntax and examples. You can also go through our other related articles to learn more –