In this Python tutorial, we will discuss several methods to convert list of tuples to string in Python. Moreover, we’ll look at a variety of examples to convert the list of tuples to a string in Python.
Recently, I am working on a machine learning project and I have found that it requires some string elements rather than a Python list of tuples. So I have done some research and found that we have to convert the list of tuples to a string in Python.
We’ll go over easy techniques that make it easy to change a Python list of tuples into a string.
Here we will learn
- Convert list of tuples to string in Python
- How to convert a list of tuples into a string using join()
- Using join() and map()
Our objective is to transform it into a String, which is a string of characters. The characters may be alphabets, certain symbols, or even integers enclosed in double or single inverted commas.
How to convert list of tuples to string in Python
In Python, there are primarily three methods that are commonly used and important to understand when converting a list of tuples to a string.
Convert list of tuples to string in Python
- In this section, we will discuss how to convert list of tuples to string in Python.
- Python has a built-in library function called strip(). By removing the leading and trailing whitespaces, letters, and symbols supplied to the strip() method, it is used to return a duplicate of the original string.
- To convert the list into a string, we are going to use the str() function.
Syntax:
string.strip(character)
Note: It is an optional parameter. When a character is passed to the strip() function, it is removed from both ends of the string.
Example:
Let’s take an example and check how to convert the list of tuples to a string in Python.
Source Code:
# Creation list of tuples
new_list = [(67,45), (22,19)]
# By using the strip() function
result=str(new_list).strip('[]')
# Display the string
print("Converted the list of tuple into string :",result)
print(type(result))
In the above code, we have created a list of tuples and then removed the list from a tuple we used the concept of the strip() function.
Here is the implementation of the following given code.
In this example, we have understood how to convert list of tuples to string in Python.
Read: Convert an integer to a string in python
How to convert list of tuples to string using join()
- In this section, we will discuss how to convert list of tuples to string in Python by using the join() method.
- Using the str.join() function, we can change a Python tuple into a Python string. The string join() method concatenates the strings in an iterable, such as a tuple, list, dictionary, or set, and returns a string.
- Since the iterable in this instance is a tuple, the join() method, which returns the string, will accept the tuple as an argument along with that we will use the list comprehension method.
Example:
Let’s take an example and check how to convert a list of tuples into a string by using the join() method in Python.
# Input list of tuples
Cars_in_USA = [("Tesla"), ("BMW")]
# By using the join() method
result= "".join([str(i) for m in Cars_in_USA for i in m])
# Display the Content
print("Conversion list of tuple into string :",result)
# Data type
print(type(result))
In the above code first, we created the list and assign the elements in the form of tuples. Now we want to convert them into a string and by using the list comprehension and join() method we can easily get the single input string.
You can refer to the below Screenshot
This is how to convert a list of tuples into a string by using the join method
Using join() and map()
- This is a different way of performing this task. Here, we convert each item to a string before using the join function to combine the tuples ().
- Each tuple is converted into a string value by the map() function, and the join() method combines the group of strings into a single string by applying the provided delimiter.
Example:
Let’s take an example and check how to convert the list of tuples into a string in Python.
Source Code:
# Creation list of tuples
Country_name = [('U.S.A'), ('Australia'), ('China'), ('Belgium')]
# Display the input list
print("Input list of tuples : ", Country_name )
#By using the map() and join() function
new_output = " ".join(map(str, Country_name))
# Display the content
print("Convert list of tuples into string :",new_output)
In the following given code first, we initialized the input list as a ‘Country_name‘ and then used the join() and map() functions to convert the input list of tuples into a string
Here is the implementation of the following given code.
Also, take a look at some more Python tutorials.
- Python convert tuple to list
- Convert float to int Python
- Python dictionary values to list
- Convert tuple to string in Python
- How to Convert a list to DataFrame in Python
In this article, we have discussed how to convert a list of tuples into a string and also we applied different methods to understand when converting a list of tuples to a string
- Conversion the list of tuples to a string in Python
- How to convert a list of tuples into a string using join()
- Using join() and map()
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.