A very elegant and fast way is to use regular expressions:
import re
str="ag ct oso gcota"
str = re.sub('[^atcg]', '', str)
"""str is now 'agctgcta"""
More Related Contents:
- Remove specific characters from a string in Python
- How to search and replace text in a file?
- Speed up millions of regex replacements in Python 3
- Best way to replace multiple characters in a string?
- How to use string.replace() in python 3.x
- String replace doesn’t appear to be working
- How to input a regex in string.replace?
- python replace single backslash with double backslash
- replacing text in a file with Python
- Removing a list of characters in string
- Replace nth occurrence of substring in string
- Python Replace \\ with \ [duplicate]
- Replace first occurrence only of a string?
- Mass string replace in python?
- Replace special characters in a string in Python
- Replacing specific words in a string (Python)
- pandas: replace string with another string
- How to remove all characters before a specific character in Python?
- Adding backslashes without escaping [duplicate]
- Mutable and Immutable Strings in python
- Python replace function [replace once]
- How do I concatenate items in a list to a single string?
- Fast punctuation removal with pandas
- Checking whether a string starts with XXXX
- Stripping non printable characters from a string in python
- Python __str__ versus __unicode__
- Removing control characters from a string in python
- How to get char from string by index?
- Format string dynamically [duplicate]
- What is internal representation of string in Python 3.x