Remove Character
Remove chosen text with replace and an empty replacement. Students distinguish deleting all matching substrings from deleting a character at a particular index.
1# Take string input2s = input("Enter a string: ")3char_to_remove = input("Enter character to remove: ")45# Remove6result = s.replace(char_to_remove, "")7print(f"After removal: {result}")
Output
Enter a string: Hello Enter character to remove: l After removal: Heo
In this one
- Delete with empty text
- Match case sensitively
- Distinguish values from indexes
How it works
- 1
Read the removal target.
- 2
Replace its nonoverlapping matches with empty text.
- 3
Print result without changing s.
Edge cases
Does an absent target raise an error?
Does an absent target raise an error?
No. The returned content is unchanged.
What if the removal target is empty?
What if the removal target is empty?
Replacing empty text with empty text leaves the content unchanged.
What's going on
replace(char_to_remove, "") removes each nonoverlapping target match. Empty replacement text means no characters take its place.
For banana and target an, the result is ba. The API can remove several-character substrings despite the prompt's singular wording. Matches are case-sensitive; an absent target leaves the content unchanged. The original s remains available because strings are immutable.
Does an absent target raise an error? No. The returned content is unchanged.
What if the removal target is empty? Replacing empty text with empty text leaves the content unchanged.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What makes this deletion?
Pick an answer to check it.
Question 2
Does an absent target raise an error?
Pick an answer to check it.
Questions people ask
Does an absent target raise an error?
No. The returned content is unchanged.
What if the removal target is empty?
Replacing empty text with empty text leaves the content unchanged.
What makes this deletion?
The replacement is an empty string.
๐Related Content
- ๐
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- ๐
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- ๐ป
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum