Python: Delete all occurrences of a specified character in a given string
Python String: Exercise-86 with Solution
Write a Python program to delete all occurrences of a specified character in a given string.
Sample Solution:
Python Code:
# Define a function to delete all occurrences of a specified character in a given string
def delete_all_occurrences(str1, ch):
# Use the replace() method to replace all occurrences of the specified character with an empty string
result = str1.replace(ch, "")
# Return the modified string
return result
# Initialize a string
str_text = "Delete all occurrences of a specified character in a given string"
# Print the original string
print("Original string:")
print(str_text)
# Print a newline for better formatting
print("\nModified string:")
# Specify the character to be deleted and call the function to delete all occurrences of that character
ch = 'a'
print(delete_all_occurrences(str_text, ch))
Sample Output:
Original string: Delete all occurrences of a specified character in a given string Modified string: Delete ll occurrences of specified chrcter in given string
Flowchart:
Python Code Editor:
Previous: Write a Python program to convert a given Bytearray to Hexadecimal string.
Next: Write a Python program find the common values that appear in two given strings.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/string/python-data-type-string-exercise-86.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics