{"id":24483,"date":"2021-10-17T18:50:43","date_gmt":"2021-10-17T13:20:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=24483"},"modified":"2021-11-05T19:25:37","modified_gmt":"2021-11-05T13:55:37","slug":"python-string-partition-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-partition-method-examples\/","title":{"rendered":"Python String partition() Method Examples"},"content":{"rendered":"

In the previous article, we have discussed Python Program for lstrip() Function<\/a>
\npartition() Function in Python:<\/strong><\/p>\n

The partition() method looks for a specified string and splits it into a tuple with three elements.<\/p>\n

The part preceding the specified string is contained in the first element.<\/p>\n

The specified string is contained in the second element.<\/p>\n

The part following the string is contained in the third element.<\/p>\n

Note: <\/strong>partition() method will look for the first occurrence of the specified string.<\/p>\n

Syntax:<\/strong><\/p>\n

string.partition(value)<\/pre>\n

Parameters<\/strong><\/p>\n

value:<\/strong> This is required. The string to look for.<\/p>\n

If the specified value is not found, the partition() method returns a tuple containing the following values: 1 – the entire string, 2 – an empty string, 3 – an empty string.<\/p>\n

For example:<\/strong><\/p>\n

gvn_str = \"welcome to python learning platform\"\r\nrslt_tupl = gvn_str.partition(\"java\")\r\nprint(rslt_tupl)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

('welcome to python learning platform', '', '')<\/pre>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given string = \"welcome to python learning platform\"\r\nGiven value = \"python\"<\/pre>\n

Output:<\/strong><\/p>\n

The result tuple after applying partition() function on the given string : \r\n('welcome to ', 'python', ' learning platform')<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given string = \"good morning all\"\r\nGiven value = \"all\"<\/pre>\n

Output:<\/strong><\/p>\n

The result tuple after applying partition() function on the given string : \r\n('good morning ', 'all', '')<\/pre>\n

Program for partition() Function in Python<\/h2>\n