Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Disemvowel

How do I check if a string has certain letters in them to delete them?

For example: The string contains the word "Traffic lights" but I want to delete every "a" and "i" letters in it and then print the string. How do I write that kind of code?

1 Answer

Steven Parker
Steven Parker
231,269 Points

Strings have a "replace" method that would work well here. And it can be chained:

print("Traffic lights".replace("a", "").replace("i", ""))  # shows "Trffc lghts"

For more details, see the official documentation for str.replace().

Note that while it's possible, I would not recommend a long "replace" chain as a solution to the "Disemvowel" challenge. You might consider using a loop to keep the code more compact and easy to read.