Convert set to String in Python

Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python.

Ways to convert set to string in Python

This tutorial will discuss how to convert a set to string and view it as the latter in Python. We can verify the result using the type() function.

Using the str() function

In Python, we use the str() function to typecast objects and convert them to strings. Internally, it invokes the __str__() function.

For example,

Output:

{8, 5, 6}

In the above example,

  • The set() function defines a set object.
  • We convert it to a string using the str() function.
  • We verify this using the type() function.

Using the repr() function

The repr() function returns the official string representation of an object. Unlike the str() function, it invokes the __repr__() method internally. We can evaluate this string as Python code using the eval() function.

We can use this method to convert a set to a string.

For example,

Output:

{8, 5, 6}

In the above example, if the source of the string is unknown then the eval() function is not considered a safe option and should be avoided. The ast.literal_eval() is a safer alternative.

Using the join() function

The join() function combines the elements of an iterable based on a specified delimiter. The final result is a string.

We can combine elements of the set using this function and view the final string.

See the code below.

Output:

8, 5, 6

In the above example, we typecast every element to a string using the str() function before combining them using the join() function.

Using the map() function

The map() function will apply a function to all the methods of an iterable. We can typecast every element into a string and combine them into a final string.

See the code below.

Output:

8, 5, 6

In the above example,

  • We apply the str() function to typecast every element to a string using the map() function.
  • We store the above mentioned elements to a list.
  • Then, we combine the elements using the join() function.

Conclusion

In this tutorial, we discussed how to convert a set to a string. The str() and repr() functions are the most straightforward methods to achieve this conversion. The join() and map() methods are a little complicated but work nevertheless.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • Convert String to Array in java
    11 December

    Convert String to Array in Java

    Table of ContentsHow to convert String to Array in JavaUsing toArray() method of SetUsing the split() method of String classUsing StringTokenizor classUsing the split() method of StringUtils classUsing split() method of Pattern classConclusionWas this post helpful? When developing applications in Java there are many cases where we will find ourselves converting data from one type […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floorWas this post helpful? Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This […]

  • Java BigDecimal to Double
    17 May

    Java BigDecimal to Double

    Learn about how to convert BigDecimal to Double in java

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using IterablesWas this post helpful? In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.DateWas this post helpful? In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    Table of ContentsUsing Instant objectUsing java.sql.DateWas this post helpful? In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.