Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping only certain characters in a string using Python?

In my program I have a string like this:

ag ct oso gcota

Using python, my goal is to get rid of the white space and keep only the a,t,c,and g characters. I understand how to get rid of the white space (I'm just using line = line.replace(" ", "")). But how can I get rid of the characters that I don't need when they could be any other letter in the alphabet?

like image 307
user2234167 Avatar asked Apr 02 '13 01:04

user2234167


People also ask

How do you exclude characters in a string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How do you exclude a character in Python?

Using translate(): translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .

How do you trim a character in a string in Python?

Use the . strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.


1 Answers

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"""
like image 128
Ionut Hulub Avatar answered Oct 12 '22 15:10

Ionut Hulub