[Python] How to initialize a set and print an empty set {} in python?

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I tried x={} but this gives a dict

    I also tried x=set() but when I do print(x) it gives output as set() and not {}

    How do I initialize and print a set such that if it is empty then it should show {} and not set()

    Example: If the set has contents, then it gets printed as {1,2,3} but if empty then I want it to show as {} (and not as set()). Does this need to be done programmatically using if len(x) = 0: then x='{}' else x ? Or is there any other approach to print an empty set as {} ?

    Continue reading...

Compartilhe esta Página