How To Convert A String To A Tuple Without Splitting In Python

Convert a string to a tuple without splitting in Python

To convert a string to a tuple without splitting in Python, you can use the split() function with spaces and commas. Follow the article to better understand.

Convert a string to a tuple without splitting in Python

Tuple in Python is a data type that stores values ​​that do not change later as a constant. Tuples are immutable, so iterating over the tuple’s elements is fast. So tuple has a slight edge in performance over other data types. Tuples can be used as keys for dictionaries. If data does not change, implementing it as a tuple will ensure that the data is write-protected.

Tuple functions are defined with parentheses. 

Syntax:

tuple( iterable )

Parameters:

  • iterable: objects with multiple iterable elements such as list, string, tuple, set, or iterators in Python.

A much-used function of the tuple() function is to convert a string into a tuple.

Example:

myString = 'learnshareit' 
 
# Use the tuple() function to convert a string to a tuple
myTuple = tuple(myString)

print('String converted to tuple:', myTuple)
print(type(myTuple))

Output:

String converted to tuple: ('l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't')
<class 'tuple'>

A string has been converted to a tuple, but you can see the string was split into multiple characters, which is very bad. So how can I convert a string to a tuple without splitting?

Use the split() function

The split() function with space as a parameter will help you convert a string to a non-characterized tuple.

Syntax:

str.split(sep, maxsplit)

Parameters:

  • sep: is the delimiter to split the original str string into small strings. If not specified, Python defaults sep to a blank character.
  • maxsplit: is the maximum number of splits. If not specified, Python defaults to an infinite number of splits.

Example:

  • Create a string.
  • Use the tuple() function to convert a string to a tuple.
  • The split() function with space as a parameter will help you convert a string to a non-characterized tuple.
myString = "learnshareit"

# Use the tuple() function to convert a string to a tuple
myTuple = tuple(myString.split(" "))

print('String converted to tuple without splitting:', myTuple)
print(type(myTuple))

Output:

String converted to tuple without splitting: ('learnshareit',)
<class 'tuple'>

Use commas at the end

To convert a string to a tuple, you can put a comma after the variable name you declare as a string.

Example:

  • Create a string. In the example below, I named the string ‘myString’.
  • Put a comma after the string name like ‘myString,’. So a string has been converted to a tuple without splitting.
myString = "learnshareit"

# Put a comma after the variable name as a string
myTuple = (myString,)

print('String converted to tuple without splitting:', myTuple)
print(type(myTuple))

Output:

String converted to tuple without splitting: ('learnshareit',)
<class 'tuple'>

Summary

Here are the methods that can help you convert a string to a tuple without splitting in Python. Let’s try these ways to get your desired results. Good luck to you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *