Python 3 – String join() Method
If you have ever come across the U.S. military alphabet, where each letter is represented by a phonetic word for clarity, you will appreciate the essence of this topic. To shorten the code and write your programs concisely, Python has provided the join() method for strings. It allows you to join a sequence of strings, considering a separator in between them, and creates a new string. In this article, we will explore the basics of the string join() method.
Syntax
The join() method in Python syntax is simple, and you can easily understand it.
str.join(iterable)
Here, the str
is the separator determining what will be used to join the strings. The iterable
parameter could be a list, tuple, or any other sequence of strings you want to join.
'_'.join(['Air', 'Baloon', 'Crashing'])
This code will return a new string that reads ‘Air_Baloon_Crashing’.
Parameter Description
Consider the following parameter details for successful execution.
str – (Mandatory) A string or where the iterable elements should be joined with str.
iterable – (Mandatory) iterates over elements that can be converted to strings.
Example
Let’s look at some examples that will illustrate the use of the join() function.
Example 1: Join Tuple Elements
Consider a tuple with elements ‘x’, ‘y’, and ‘z.’ The join() method helps concatenate elements of the tuple into a string with a separator ‘-‘.
# Tuple characters
characters = ('m', 'a', 'n', 'a', 'g', 'e', 'r')
# Join Tuple characters
joined_characters = '-'.join(characters)
print(joined_characters)
Output:
m-a-n-a-g-e-r
In this case, the first statement shows the creation of a tuple that has strings ‘m,’ ‘a,’ ‘n,’ and so on. The second statement uses the join() method to connect the character elements within the tuple, using a separator ‘-‘ stated within the function.
Example 2: Join List Elements
Similar to how the join() function in Python works with tuple elements, it also concatenates the given string list into a new string.
string = ["We", "are", "fanatical", "geeks"]
joined_string = " ".join(string)
print(joined_string)
Output:
We are fanatical geeks
This code defines a list string through four strings, We, are, fanatical, and geeks, and passes it to the join() method. The separator is a space between each word.
Example 3: Join Set Elements
You can also join set elements, where a set is an unordered, distinct list of elements that can be iterated.
# Define a set
strings = {"Python", "Go", "Java"}
# Join set elements
joined_strings = "_".join(strings)
print(joined_strings)
Output:
Java_Go_Python
In this example, we instantiate a set, strings, encapsulating three strings – ‘Python,’ ‘Java,’ and ‘Go.’ The join() operator concatenates their values into a string, using an underscore as a separator.
Example 4: Join a Numeric array
You can pass numeric arrays to the join() function in Python. However, you need to ensure that the array contains string values or the values capable of being converted to strings.
my_array = [100, 200, 300, 400]
separator = "_"
result = separator.join(map(str, my_array))
print(result)
Output:
100_200_300_400
This code creates an array that contains numeric values, rather than a string. However, the use of the map() method converts each value to a string before passing it to the join() method, with the underscore as the separator.
Quick Notes on the join() Method
Join() method is only applicable to string types.
The join method returns a new string.
Join() can work with any iterable in Python.
In conclusion, the join() method offers you an easy and efficient way to concatenate a sequence of strings in Python. You can use it with no limitations in your projects to keep your code clean, concise, and legible.