python

Python – Convert a Tuple to a String

In this tutorial, we are going to see how to convert a Tuple to a String in Python. Python provides different types of variables for programmers. We can use data types like int, float, string, list, set… in our applications. When using different types of variables, it may be necessary to convert these to different types.
 

 

Convert a Tuple to a String Using join()

The join() method takes all the elements of an iterable and joins them into a single string.

tuple = ('a', 'b', 'c', 'd', 'e', 'f') 
# combine all characters in the tuple
str =  ''.join(tuple)
# display the string
print(str)

Output:

abcdef

 

Specify different delimiters

So far, we’ve used the space as a separator between items. But we can specify different delimiters by changing the space with a new delimiter.

tuple = ('a', 'b', 'c', 'd', 'e', 'f') 
# combine all characters in the tuple
str =  ', '.join(tuple)
# display the string
print(str)

Output:

a, b, c, d, e, f
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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