| by Arround The Web | No comments

Python String Join() Method

To merge the string elements, the Python string join() method is used. As a result, it returns a string that concatenates the strings in iterable like tuples, dictionaries, and sets. After merging the parts of the string, this method inserts the separators between them to make sense. Without separators, the string cannot be understandable. Thus, this function inserts the separators to make the string easy to read or understand. Any symbol such as a “comma”, “space”, or any other, may be used as a separator.

Syntax of the Python String Join() Method:

Example 1: Using the String Join() Python Method

In this example, several separators are used to join or concatenate a string using the string join() method. Using parentheses or a specified divider string, the join() method concatenates each item in a list or object that resembles an array and gives a new string. If there is only one component in the collection, the separator will not be utilized when returning the item.

Let’s begin with the first example, in which the string values are concatenated together without the use of separators. At first, there is a list of strings with various string values. The string list consists of four values. The string’s values are “oop”, “java”, and “php”. We keep this list in the already initialized “subjects” variable. The join() method is then invoked with a “subject” variable in the next line, and there is nothing in place of the separator because we merge the strings without a separator in this section of the code.

The variable “s” that we initialized before contains the results of the join() procedure. To simply combine the string values, we utilize an empty separator. Next, we use the print function with the variable “s” in the following line to show the outcome on a screen.

It outputs a string that is made up of every character in the iterable. Since we set the separator as “empty” in the script, there is no space or other separator used between the values of the string as can be seen in the following image. It now contains “oopjavaphp” in the string.

The second section of the script involves joining the strings using the join() method and inserting a space between each value from the list of strings. Let’s begin writing the code. First, we have a list of three strings – “I”, “Love”, and “Coding” which we store in the variable “M” that we already initialized.

Then, we use the print() function. Within this function, we use the join() method to join the string elements that are present in a list using the space as a separator in between these items of string. We use the separator space by pressing the space key from the keyboard in between the inverted commas, and we added a join() function with it. Inside the parentheses of the join() method, we put the variable “M” because our list of string elements is stored in it.

The string list items are now merged using a separator between them. As you can see, it inserts a separator space between them to return a new string, “I Love Coding”.

The components of the tuple are concatenated in the third section of the code. To store the various values in a single variable, use tuples. The tuple dataset is one of Python’s structure plan datasets to store the large datasets. The last three are collection, set, and dictionary, each of which has a distinct set of traits and uses. A tuple is a continuous collection that is arranged. Starting with the first line of code, we have a list of tuples with the values “Alex”, “24”, and “1998”. As we know that string values must be placed in between inverted commas and that integer values should not be written in between the inverted commas. The join function is then used in the next line with the separator “,”. In the join() method’s parenthesis, we enter a variable “list” since our tuple list is stored in this variable, which we have to join. The result of the join() method is placed in the variable “S” that we initialized before. Therefore, we then use the print function to print the result.

When we hit the run button to view its output, a “TypeError” message appears because there are both integer and string values in the list of tuples. The sting join() method can only join the values that are strings, so we must convert the list to strings before concatenating.

As we can see, the list contains the values “Alex”, “24” and “1998”. We passed the variable “a” to the string function so “a” places each value one at a time to perform the join() function on it. Then, because the result is kept in the variable “S”, we invoke the print function by passing the argument “S” to display the result.

You can now see that the list of tuples is transformed into a string and the output is presented, following the merging of the string and the usage of the “comma” as a separator.

Example 2: Utilizing the Join() Method to Join the String with Sets

In this example, we’ll use the sets with join() method to combine the string elements. Use the sets to hold many items in a string parameter. The set in Python is used to hold the data collections. A set is an unsorted, unalterable, and unindexed collection.

Let’s start the program. This program’s first line contains a list of strings with the values “Noah”, “Alex”, “Smith” and “John” in it. The symbol that we want to utilize as a separator which is the underscore ” ” is then initialized and stored in the variable “s”. Then, in the following line, we use the join method with a separator that is contained in “s”. So, we use the “s.join()”. In the join method’s parenthesis, we use the variable “Name” as it is this variable that contains the list that we want to concatenate. We use the result variable to store the outcome of merging this list. To display the output on the screen, we pass the variable “result” as a parameter to the print() function.

The elements in the string list are combined, but we can see that their order has changed. Thus, in essence, this is what the “sets” truly do: they alter the order of the sequence of the elements after the merger and if any element in the list repeats, they only show up once. Because we used underscore as a separator between these elements, it also used underscore between them.

Example 3: Implementing the Join() Method to Join the String with a Dictionary

In this example, we’ll join a string with a dictionary, which means that we’ll join the dictionary’s keys rather than its contents. The iterable of the type “dictionary” contains the key-value pairs of the data elements. With the use of a string separator, the join() method in Python only connects the dictionary’s key portion. The value part of the join is absent.

The first item that we have is “success:1” in the dictionary which contains the key-value-pair that represents the pair of data items at the start of the code. As shown in the following image, we can also see the other “key-value” that are included in the dictionary. We have a total of six key values on the list.

Always keep in mind that the dictionary key must be a string. Otherwise, “TypeError” message appears in the output display. Then, since the items are stored in the variable “dictionary”, we supplied it as a parameter to the join() method along with the separator underscore “_” which we used to join the keys by inserting an underscore in between them. The result of joining the keys is then placed in the print function(), which is then called while passing the argument “string” along with it.

The dictionary merely connects the keys and ignores the values so the keys are correctly joined. The underscore character “_” is used to combine the keys.

Conclusion

We learned about using the Python string join() method to combine the items of a list of strings. After integrating the elements, we added some separators like “comma”, “space”, and “underscore” between them. In the first example, we joined the string elements using the join() method, which returns the result as a single string after joining the elements. Next, in the second example, we merged the elements with sets using the join() method. Finally, in the third example, we joined the string using the join() method with dictionaries.

Share Button

Source: linuxhint.com

Leave a Reply