{"id":16896,"date":"2021-08-17T08:48:02","date_gmt":"2021-08-17T03:18:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=16896"},"modified":"2021-11-22T18:37:23","modified_gmt":"2021-11-22T13:07:23","slug":"python-program-for-comma-separated-string-to-tuple","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-comma-separated-string-to-tuple\/","title":{"rendered":"Python Program for Comma-separated String to Tuple"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Shuffle Elements of a Tuple<\/a> A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.<\/p>\n split() function:<\/strong><\/p>\n Syntax:<\/strong> input string.split(seperator=None, maxsplit=-1)<\/strong><\/p>\n It delimits the string with the separator and returns a list of words from it. The maxsplit parameter specifies the maximum number of splits that can be performed. If it is -1 or not specified, all possible splits are performed.<\/p>\n Example:\u00a0 Given string =\u00a0 ‘hello btechgeeks goodmorning’.split()<\/p>\n Output : [‘hello’, ‘btechgeeks’, ‘goodmorning’ ]<\/p>\n Given a string and the task is to find a tuple with a comma-separated string.<\/p>\n Examples:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find a tuple with a comma-separated string.<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Shuffle Elements of a Tuple Tuple in Python: A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running. split() function: Syntax: input string.split(seperator=None, maxsplit=-1) It delimits the string with the …<\/p>\n
\nTuple in Python:<\/strong><\/p>\nGiven String = 'good, morning, btechgeeks' (static input)<\/pre>\n
Given input String = good, morning, btechgeeks\r\nA tuple with comma-separated string is : ('good', ' morning', ' btechgeeks')<\/pre>\n
Given String = hello, btechgeeks, 123 (user input)<\/pre>\n
A tuple with comma-separated string is : ('hello', ' btechgeeks', ' 123')<\/pre>\n
Program for Comma-separated String to Tuple<\/h2>\n
\n
Method #1: Using split() Method (Static input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = 'good, morning, btechgeeks'\r\n# Print the above-given input string.\r\nprint(\"Given input String = \", gvn_str)\r\n# Split the given input string separated by commas using the split() function\r\n# and convert it into a tuple.\r\n# Store it in another variable.\r\ntupl = tuple(gvn_str.split(\",\"))\r\n# Print the tuple with a comma-separated string.\r\nprint(\"A tuple with comma-separated string is : \", tupl)\r\n<\/pre>\n
Given input String = good, morning, btechgeeks\r\nA tuple with comma-separated string is : ('good', ' morning', ' btechgeeks')<\/pre>\n
Method #2: Using split() Method (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# Print the above-given input string.\r\nprint(\"Given input String = \", gvn_str)\r\n# Split the given input string separated by commas using the split() function\r\n# and convert it into a tuple.\r\n# Store it in another variable.\r\ntupl = tuple(gvn_str.split(\",\"))\r\n# Print the tuple with a comma-separated string.\r\nprint(\"A tuple with comma-separated string is : \", tupl)\r\n<\/pre>\n
Enter some random string = hello, btechgeeks, 123\r\nGiven input String = hello, btechgeeks, 123\r\nA tuple with comma-separated string is : ('hello', ' btechgeeks', ' 123')<\/pre>\n
\n