If you have a set and you want to append a prefix or a suffix to each element of that set then you can make use of the set comprehension in Python.
Syntax:Example:
cities = {"New York", "London", "Paris", "Tokyo", "Sydney", "Nairobi", "Dubai"}
welcome_cities = {"Welcome to " + city for city in cities}
for city_greeting in welcome_cities:
print(city_greeting)
Output:
Example:
prices = {1.99, 5.49, 7.25, 12.50, 3.75}
prices_with_dollar = {"$" + str(price) for price in prices}
print(prices_with_dollar)

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!