How to Use String partition() Method in PythonPython Basics


The partition() method splits the string at the first occurrence of the argument string.

Example

str = "Python is a popular languages and it is good for data analysis"

print(str)
print(str.partition('is '))
print(str.partition('not '))

Output:

Python is a popular languages and it is good for data analysis
('Python ', 'is ', 'a popular languages and it is good fordata analysis')
('Python is a popular languages and it is good for data analysis', '', '')

Syntax

str.partition(separator)

Parameters

The method takes a string (separator) as the parameter.

Return Value

It returns a 3-tuple containing the part before separator, separator, and the part after separator.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us