{"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> 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 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 Output:<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples<\/a> Tutorial & meet such challenges with ease.<\/p>\n In the previous article, we have discussed Python Program for lstrip() Function partition() Function in Python: The partition() method looks for a specified string and splits it into a tuple with three elements. The part preceding the specified string is contained in the first element. The specified string is contained in the second element. The …<\/p>\n
\npartition() Function in Python:<\/strong><\/p>\nstring.partition(value)<\/pre>\n
gvn_str = \"welcome to python learning platform\"\r\nrslt_tupl = gvn_str.partition(\"java\")\r\nprint(rslt_tupl)\r\n<\/pre>\n
('welcome to python learning platform', '', '')<\/pre>\n
Given string = \"welcome to python learning platform\"\r\nGiven value = \"python\"<\/pre>\n
The result tuple after applying partition() function on the given string : \r\n('welcome to ', 'python', ' learning platform')<\/pre>\n
Given string = \"good morning all\"\r\nGiven value = \"all\"<\/pre>\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
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"welcome to python learning platform\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"python\"\r\n# Apply partition() function to the given string for the given value to split\r\n# the given string into a tuple with three elements as mentioned above.\r\n# Store it in another variable.\r\nrslt_tupl = gvn_str.partition(gvn_valu)\r\n# Print the result tuple after applying partition() function on the given string\r\n# for the given value\r\nprint(\"The result tuple after applying partition() function on the given string : \")\r\nprint(rslt_tupl)\r\n# Similarly, do the same for the other string and print the result string\r\ngvn_str2 = \"good morning all\"\r\ngvn_valu2 = \"all\"\r\nrslt_tupl2 = gvn_str2.partition(gvn_valu2)\r\nprint(\"The result tuple after applying partition() function on the given second string : \")\r\nprint(rslt_tupl2)\r\n<\/pre>\n
The result tuple after applying partition() function on the given string : \r\n('welcome to ', 'python', ' learning platform')\r\nThe result tuple after applying partition() function on the given second string : \r\n('good morning ', 'all', '')<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the value as user input using the input() function and\r\n# store it in another variable.\r\ngvn_valu = input(\"Enter some random value = \")\r\n# Apply partition() function to the given string for the given value to split\r\n# the given string into a tuple with three elements as mentioned above.\r\n# Store it in another variable.\r\nrslt_tupl = gvn_str.partition(gvn_valu)\r\n# Print the result tuple after applying partition() function on the given string\r\n# for the given value\r\nprint(\"The result tuple after applying partition() function on the given string : \")\r\nprint(rslt_tupl)\r\n# Similarly, do the same for the other string and print the result string\r\ngvn_str2 = input(\"Enter some random string = \")\r\ngvn_valu2 = input(\"Enter some random value = \")\r\nrslt_tupl2 = gvn_str2.partition(gvn_valu2)\r\nprint(\"The result tuple after applying partition() function on the given second string : \")\r\nprint(rslt_tupl2)\r\n<\/pre>\n
Enter some random string = this is python coding platform\r\nEnter some random value = coding\r\nThe result tuple after applying partition() function on the given string : \r\n('this is python ', 'coding', ' platform')\r\nEnter some random string = hello all 12345\r\nEnter some random value = 12345\r\nThe result tuple after applying partition() function on the given second string : \r\n('hello all ', '12345', '')<\/pre>\n
\n