To join the elements of a set into a string in Python. we can use the join() function or create a function that can do so yourself. Read the following article and then discuss this topic together.
Join the elements of a set into a string in Python
Set in Python is a data structure related to set math, also known as set theory. A set can contain many elements, and these elements have no order. The elements stand indiscriminately in the set. The elements of a set are immutable data types such as int, list, or tuple.
To declare a set: setObj = { element 1, element 2,......}
To join the elements of a set into a string in Python, I have the following ways:
Use the join() function
Syntax:
str.join(object)
Parameters:
- str: Delimiter for string concatenation.
- object: List or tuple containing elements that are strings to be concatenated.
The join() function returns an object that is a character string of the elements of the specified list or tuple joined together by a delimiter.
Example:
- Initialize a collection.
- Use the join() function to join the elements of the set into a string.
fruits = {'apple', 'pear', 'orange'} # Use the join() function to concatenate elements in a set into a string myString = ''.join(fruits) print('The elements in the set are concatenated into a string:', myString)
Output:
The elements in the set are concatenated into a string: pearorangeapple
You can pass additional delimiters to the string.
Example:
fruits = {'apple', 'pear', 'orange'} # Use the join() function to concatenate elements in a set into a string myString = '#'.join(fruits) print('The elements in the set are concatenated into a string:', myString)
Output:
The elements in the Set are concatenated into a string: orange#pear#apple
Declare a custom function
You can write a function that concatenates the elements of a set into a string.
Example:
fruits = {'apple', 'pear', 'orange'} def concatString(fruitParam): result = '' for f in fruitParam: result += f return result print('The elements in the set are concatenated into a string:', concatString(fruits))
Output:
The elements in the set are concatenated into a string: orangeapplepear
In my example, I have initialized a function named ‘concatString
‘. In that function, I have to write one loop to concatenate each set element into a string. The variable result has the effect of saving and returning that string.
If you want to customize the length of the string, you can add the slice() function to the concatString
function.
Example:
fruits = {'apple', 'pear', 'orange'} def concatString(fruitParam): result = '' for f in fruitParam: result += f # Use the slice() function return result[0:] print('The elements in the set are concatenated into a string:', concatString(fruits))
Output:
The elements in the set are concatenated into a string: pearorangeapple
Change the length by changing the starting index like [1:] or [2:].
Summary
To join the elements of a set into a string in Python, the quick and convenient way is to use the join() function. What do you think about this topic? Please leave a comment below.
Maybe you are interested:
- Join The Keys Of A Dictionary Into A String In Python
- Sort a list of tuples by the second element in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java