- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I convert Python strings into tuple?
We can convert a python string into tuple by simply mentioning a comma (,) after the string. This will treat the string as a single element to the tuple. Here our string variable “s” is treated as one item in the tuple, which can be done by adding the comma after the string.
Example
s = "python" print("Input string :", s) t = s, print('Output tuple:', t) print(type(t))
Output
Following is the output of the above program
Input string : python Output tuple: ('python',) <class 'tuple'>
Using tuple() function
Also we can use the tuple() function to convert the given string into a tuple. The tuple() is a python built−in function which is used to create a tuple from an iterable.
Example
Here the tuple() function assumes that each character of the string represents an individual item.
s = "python" print("Input string :", s) result = tuple(s) print('Output tuple:', result)
Output
Input string : python Output tuple: ('p', 'y', 't', 'h', 'o', 'n')
Using string.split() method
If the input string has space separated characters and we want only those characters then we can use the string.split() method to avoid calculating white space as an element.
The string.split() method splits the given data into different sections based on the default delimiter (white space “ ”) or specified delimiter. It returns a list of string elements which are separated based on the specified delimiter.
Example
In the following example the string with space separated characters are splitted and then converted to tuple successfully by using string.split() and tuple() functions.
s = "a b c d e" print("Input string :", s) result = tuple(s.split()) print('Output tuple:', result)
Output
Input string : a b c d e Output tuple: ('a', 'b', 'c', 'd', 'e')
Example
In the following example the elements of the string are separated by “@” character and we have separated the elements by specifying s.split(“@”) to separate the elements of the string and then it is converted to tuple.
s = "a@b@c@d@e" print("input string :", s) result = tuple(s.split("@")) print('Output tuple:', result)
Output
input string : a@b@c@d@e Output tuple: ('a', 'b', 'c', 'd', 'e')
Using map() and int() functions
If the given string has numeric values represented in string then the converted tuple elements are also represented in string format only, if we want to convert the type tuple elements then we need to use the map() and int() functions together.
map(): The map function used to apply the given function to every element of the iterable.
Int(): The int() function returns a converted integer object from the given string/number.
Example
s = "1 2 3 4 5" print("Input string :", s) result = tuple(map(int, s.split(" "))) print('Output tuple:', result) print("Type of tuple element: ", type(result[1]))
Output
Input string : 1 2 3 4 5 Output tuple: (1, 2, 3, 4, 5) Type of tuple element: <class 'int'>
By using the above ways we can successfully convert the python string into a tuple.