- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete a character from a string using python?
If you want to delete a character at a certain index from the string, you can use string slicing to create a string without that character. For example,
>>> s = "Hello World" >>> s[:4] + s[5:] "Hell World"
But if you want to remove all occurances of a character or a list of characters, you can use the following methods:
The string class has a method replace that can be used to replace substrings in a string. We can use this method to replace characters we want to remove with an empty string. For example:
>>> "Hello people".replace("e", "") "Hllo popl"
If you want to remove multiple characters from a string in a single line, it's better to use regular expressions. You can separate multiple characters by "|" and use the re.sub(chars_to_replace, string_to_replace_with, str). For example:
>>> import re >>> re.sub("e|l", "", "Hello people") "Ho pop"
Note: You can also use the  to create group of characters to replace in regex.
- How to delete consonants from a string in Python?
- How to delete the vowels from a given string using C language?
- How to remove a particular character from a String.
- How to delete a file using Python?
- How to match a character from given string including case using Java regex?
- How to extract numbers from a string using Python?
- Removing nth character from a string in Python program
- Python program for removing nth character from a string
- How to delete a row from a table using jQuery?
- Python program to change character of a string using given index
- Python Pandas - How to delete a row from a DataFrame
- How to find a unique character in a string using java?
- How can you delete a record from a table using MySQL in Python?
- How to delete lines from a Python tkinter canvas?
- Python program for removing n-th character from a string?