Python string to tuple – Python Program for Comma-separated String to Tuple

Python string to tuple: 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)

Python split tuple: 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.

Example:  Given string =  ‘hello btechgeeks goodmorning’.split()

Output : [‘hello’, ‘btechgeeks’, ‘goodmorning’ ]

Given a string and the task is to find a tuple with a comma-separated string.

Examples:

Example 1:

Input: 

Given String = 'good, morning, btechgeeks'     (static input)

Output:

Given input String =  good, morning, btechgeeks
A tuple with comma-separated string is :  ('good', ' morning', ' btechgeeks')

Example 2:

Input:

Given String = hello, btechgeeks, 123        (user input)

Output:

A tuple with comma-separated string is : ('hello', ' btechgeeks', ' 123')

Program for Comma-separated String to Tuple

Split a tuple python: Below are the ways to find a tuple with a comma-separated string.

Method #1: Using split() Method (Static input)

Approach:

  • Give the string as static input and store it in a variable.
  • Print the above-given input string.
  • Split the given input string separated by commas using the split() function and convert it into a tuple.
  • Store it in another variable.
  • Print the tuple with a comma-separated string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'good, morning, btechgeeks'
# Print the above-given input string.
print("Given input String = ", gvn_str)
# Split the given input string separated by commas using the split() function
# and convert it into a tuple.
# Store it in another variable.
tupl = tuple(gvn_str.split(","))
# Print the tuple with a comma-separated string.
print("A tuple with comma-separated string is : ", tupl)

Output:

Given input String =  good, morning, btechgeeks
A tuple with comma-separated string is :  ('good', ' morning', ' btechgeeks')

Method #2: Using split() Method (User input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Print the above-given input string.
  • Split the given input string separated by commas using the split() function and convert it into a tuple.
  • Store it in another variable.
  • Print the tuple with a comma-separated string.
  • The Exit of the program.

Below is the implementation:

#Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Print the above-given input string.
print("Given input String = ", gvn_str)
# Split the given input string separated by commas using the split() function
# and convert it into a tuple.
# Store it in another variable.
tupl = tuple(gvn_str.split(","))
# Print the tuple with a comma-separated string.
print("A tuple with comma-separated string is : ", tupl)

Output:

Enter some random string = hello, btechgeeks, 123
Given input String = hello, btechgeeks, 123
A tuple with comma-separated string is : ('hello', ' btechgeeks', ' 123')

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.