If you have a String in Python and you want to delete a specific character from it, you can do that by using the replace() method.
Let's take a look at an example.
string = "This is so Cool!"
Let's say you want to delete the letter capital C
new_string = string.replace("C","") print(new_string)
string = "This is so ool!"
Note, if the character is not unique, the replace method will replace all the occurrences here with blank and shift all other letters by one to the left.
There is a solution to this. You can add another optional parameter that can help you delete one or more occurrences.
So there is there character "o" in our example, let's look at an example with it.
new_string_remove_first_o = string.replace("o","",1) new_string_remove_second_o = string.replace("o","",2) new_string_remove_third_o = string.replace("o","",3) print(new_string_remove_first_o) print(new_string_remove_second_o) print(new_string_remove_third_o)
This is s Cool! This is s Col! This is s Cl!
Facing issues? Have Questions? Post them here! I am happy to answer!
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
- How to open a file via Mac Terminal - MacOS
- How to install PowerShell on macOS - Powershell
- 12 August - International Youth Day celebrated worldwide - News
- List of Special Parameters/Variables in Bash Shell Scripting - Linux
- Fix Error - Another active Homebrew update process is already in progress - MacOS
- Python: Read a file into a List - Python
- Google YouTube Gmail down, not working worldwide - 503, 502, something went wrong error - News
- Error: Unable to access jarfile jarFileName.jar file [Windows] - Java