Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python partition

by suresh

The Python partition is one of the Python String Method, which is useful to split the given string using the specified separator and return a tuple with three arguments. This Python partition function starts looking for the separator from the Left-Hand side. Once it finds the separator, partition function returns the string before the Separator as Tuple Item 1, Separator itself as Tuple Item 2, and remaining string (or string after the Separator) as Tuple Item 3.

Syntax of a partition Function in Python

The syntax of the partition method in Python Programming Language is

String_Value.partition(Separator)
  • String_Value: Please select the valid String variable, or you can use the String directly.
  • Separator: This argument is required, and if you forget this argument, python throw TypeError.

NOTE: If you pass the non-existing item as a separator, the Python partition function return the whole string as Python Tuple Item 1 followed by two empty tuple items.

Return Value: Python partition function returns Tuple with three arguments. For example, If we have A*B*C and If we use * as a separator. The Python partition function search for * from left to right. Once partition function finds * it returns the string before the * symbol as Tuple Item 1 (A), * as Tuple Item 2 and remaining string as Tuple Item 3 (B*C).

Even though there are multiple occurrences of the separator, the partition function looks for the first occurrence

Python partition method Example

The following set of examples help to understand the partition Function in Python Programming Language.

# Python Partition Method Example
 
Str1 = 'Tutorial Gateway'
Str2 = 'Learn-Python-Programming'
Str3 = '[email protected]'
 
Str4 = Str1.partition(' ')
print("After Partitioning String 1 = ", Str4)

Str5 = Str2.partition('-')
print("After Partitioning String 2 = ", Str5)

Str6 = Str3.partition('@')
print("After Partitioning String 3 = ", Str6)

#Performing Python Partition function directly
Str7 = 'First_Image.png'.partition('.')
print("After Partitioning String 7 = ", Str7)
Python Partition Method

TIP: Please refer to Python rpartition article in Python String Method to understand the rpartition method, which performs the same operation but from right to left.

In this partition function example, first, we declared three String variable Str1, Str2, Str3. Next, we assigned corresponding value using the following statement

Str1 = 'Tutorial Gateway'
Str2 = 'Learn-Python-Programming'
Str3 = '[email protected]'

The following statement partition the Str1 string into multiple parts based on the separator we specified (i.e., Empty Space) and prints the output.

Str4 = Str1.partition(' ')
print("After Partitioning String 1 = ", Str4)

It partitions the Str2 string into multiple parts based on the ‘_’ symbol and prints the output.

Str5 = Str2.partition('-')
print("After Partitioning String 2 = ", Str5)

The following Python partition statement partition the Str3 string into multiple parts based on the ‘@’ symbol and prints the output.

Str6 = Str3.partition('@')
print("After Partitioning String 3 = ", Str6)

The below code uses the partition function directly on String

Str7 = 'First_Image.png'.partition('.')
print("After Partitioning String 7 = ", Str7)

The below Python code looks for *, which doesn’t exist. So it returns the Tuple as (‘Tutorial Gateway’, ”, ”).

Str8 = Str1.partition('*')

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy