| by Arround The Web | No comments

Python String Partition() Method

The “partition()” method of the Python string splits a string into three elements, the first of which is the value before the separator, the second is the provided separator, and the third is the value after the separator.

Syntax of the Python String Partition() Method:

Example 1: Using the String Partition() Method in Python

The Python string “partition()” function is used in this example in which a string returns a value into a tuple with three elements. The tuple object returns three elements, the first of which is a substring that comes before the provided string, the second is the specified string, and the third is another substring that comes after the provided string. In this case, the supplied string essentially serves as a separator. The element that we choose as a separator from the supplied string is the requested string.

The string is then divided into a tuple using the “partition()” method. Multiple items can be kept in a single object by using tuples. One of Python’s built-up area datasets for keeping the data collections is the tuple; the other three are collection, set, and dictionary, each with a distinct set of features and applications. A tuple is an immutable, ordered collection.

Let’s start with the first instance where we have the string value “My favorite programming language is Python” that we stored in the variable “string” that we initialized earlier. Then, in the following line, we use the string “partition()” method with the argument “string” because the string value is stirred in it. In addition, we pass the separator, which is also known as a specified operator “language” to the partition method. And we store the “partition()” result in the “outcome” variable. This separates the string into three-element tuples. Then, to display the outcome on the screen, we use the “print()” function in the next line with “outcome” as its parameter.

Now that the string is split into a tuple with three elements, we can see it in the following output image. Due to the separator used as “language” in the previous script, the given strings now include three items. The separator element is “language” followed by the substring “Python”. And then, the first element is “My favorite programming” which is a substring that comes before the specified term, “language”.

In the Python “partition()” method for strings, we can also utilize the numbers as a defined string or separator. Therefore, in the second section of the code, we split the string into a tuple using the numerical digit as a separator. Start the code now. Beginning with the string value “Alex got 78 marks and Smith got 88 marks in their exam”, we initialize the variable “string1” to hold this value. The “partition()” function is then used in the next line with the “string1” as its parameter and the value “78” which is the number in the parenthesis of the partition method. The outcome variable, which stores the output of the “partition()” method, is then passed to the “print()” function.

The string is divided into a tuple, which now contains three pieces. The “partition()” method divides a string into three elements as shown in the previous illustration. Since “78” is used as a separator, the first element is “Alex got” before the “78”. The second element is the actual “78” and the third element is the third part, which is the part after the separator, which is “marks and Smith got 88 marks in their exam”.

Using the string “partition()” technique, we can also divide the string using symbols as a separator. In this third section of the code, we use the “%” symbol as a separator to divide the string into two parts. However, you can use any other symbol that you like.

We have the string value “Noah got 80% in his result” in the script, and we stored it in the variable “string2”. Next, we use the “partition()” function with “string2” as an argument because this is where the value of the string is stored that needs to be split. We do so by using the separator “%” inside the “partition()” method. Ultimately, we use the “print()” function and pass the variable “outcome” as its argument which we previously initialized. The result of the “partition()” method is stored in it.

As a result, a tuple composed of three items is displayed. The first element is “Nah got 80” which is the part before the separator “%” in the preceding specification string. The last element is the section following the separator, which is “in his result”. Before that, it displays “%” which is the requested string. The elements in the tuple always appear between the inverted commas. If there is a comma after that, it denotes that a single string element has ended.

The Python string “partition()” method returns a “ValueError” if we use an empty string as a separator. The string value “I love to play” is stored in the variable “str” which we must initialize first. Then, in the following line, we call the “print()” function. Inside this function, we pass the partition() method with “str1” as its argument because the string value is stored in it. Inside the parentheses of the partition() method, we use an empty separator. Now, press the run button to see it displayed on the screen.

Because we didn’t utilize the specified string in this code as a separator, it displayed a “ValueError” in the output, which is why the error message appeared on the screen.

If the supplied string cannot be found in the string, Python’s “partition()” method provides a list with three parts – the actual string, and two empty strings.

Now, let’s begin the fifth section of this code. In this code, the string value “I love coding” is stored in the created variable “str1”. The “partition()” method is then invoked with the input “str1” in the next line. The separator “to” which is not present in the string is then passed to the “partition()” method. We used the print() function to show its results on the screen.

If the supplied string cannot be found in the string, Python’s “partition()” method provides a list with three parts – the actual string, and two empty strings.
Now, let’s begin the fifth section of this code. In this code, the string value “I love coding” is stored in the created variable “str1”. The “partition()” method is then invoked with the input “str1” in the next line. The separator “to” which is not present in the string is then passed to the “partition()” method. We used the print() function to show its results on the screen.

Example 2: Using Python’s String Partition() Method to Return Two Distinct Tuples from a String Value

In this example, we’ll utilize two distinct separators for the same string to create two different tuples that each contain a different string element.

Now, let’s start with the second example’s code. We store the string value “We drink coffee every morning” in the first line and store it in the “string” variable. Then, we come to its main section where we print the two different tuples. To accomplish this, we utilize the “print()” function inside this function. We call the “partition()” method with “string” as its parameter because the value of the string is specified here. We also pass the separator “coffee” as a parameter to this method’s parentheses.

Then, in the following line, we repeat the process. But this time, the separator is changed to “every”. The first element of the tuple is empty if the separator is discovered in the beginning and the other element is the first string contained in the string list while the other is regarded as the third string element.

As in the aforementioned code, the first separator is “coffee”. Therefore, the first element in the first tuple is “We drink” which is placed before the separator, followed by the separator “coffee”, and lastly by the last element, “every morning” which is placed after the separator. Similar to the first tuple, the second tuple’s separator changed the values of the strings. We used “every” as a second separator. Thus, the first element in the tuple, “We drink coffee”, is placed before the separator. And the final element, “morning”, is placed after it.

Conclusion

We looked at the Python string “partition()” method which returns a tuple by dividing a string into three components. Employing this technique, we divided the string into tuples using a separator. We discussed how to use this method with different separators such as “numbers”, “symbols”, or a “string value”. In the first example, we mentioned that if the separator is empty, it returns “TypeError”. And in the last example, we covered how to return two separate tuples from the same string value using two different separators.

Share Button

Source: linuxhint.com

Leave a Reply