C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String partition() MethodPython partition() method splits the string from the string specified in parameter. It splits the string from at the first occurrence of parameter and returns a tuple. The tuple contains the three parts before the separator, the separator itself, and the part after the separator. It returns an empty tuple having seperator only, if the seperator not found. The method signature is given below. Signaturepartition(sep) Parameterssep: A string parameter which separates the string. ReturnIt returns a tuple, A 3-Tuple. Let's see some examples of partition(sep) method to understand it's functionality. Python String partition() Method Example 1First, let's see simple use of partition method. # Python partition() method example # Variable declaration str = "Java is a programming language" # Calling function str2 = str.partition("is") # Displaying result print(str2) # when seperate from the start str2 = str.partition("Java") print(str2) # when seperate is the end str2 = str.partition("language") print(str2) # when seperater is a substring str2 = str.partition("av") print(str2) Python String partition() Method Example 2if the separator is not found, It returns a tuple containing string itself and two empty strings. See the example below. # Python partition() method example # Variable declaration str = "Java is a programming language" # Calling function str2 = str.partition("not") # Displaying result print(str2) Output: ('Java is a programming language', '', '')
Next TopicPython Strings
|