MEhsan
MEhsan

Reputation: 2324

Replace individual character elements of string

I need help in how to identify a character from the user input? And how to replace with specific character of my own.

The Task:

  1. This program prompts the user to enter a DNA sequence (A,T,C,G) and display the reverse complement of that DNA sequence

  2. The program should only accept the DNA bases (A,T,C,G) uppercase letter only, if not, convert the character to 'x'

My problem is in the second step of the program, in how to replace the letters that are not upper-cased (A,C,G,T) with 'x' character

seq= input('Enter a DNA sequence ')

seqcom = {'A':'T','C':'G','T':'A','G':'C'} # dictionary 

letters = list(seq)
letters.reverse ()
dna =''

for base in letters:
    dna += seqcom[base]


print('The reverse complement of \n', seq, '\n', 'is\n', dna)

Upvotes: 2

Views: 223

Answers (1)

Eric
Eric

Reputation: 97565

You want: dict.get(key[, default])

for base in letters:
    dna += seqcom.get(base, 'x')

Upvotes: 3

Related Questions