Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if a letter is present in a string in python [duplicate]

Tags:

python

How can I check if the letter "N" is present in a string. Example:

flag = False
if string contains N:
   flag = True

So flag = True if string is "CNDDDNTD" and flag = False if string is "CCGGTTT". I think re.search will work, but not sure of the options to use.

like image 372
Ssank Avatar asked Jul 18 '16 18:07

Ssank


1 Answers

>>> 'N' in 'PYTHON'
True
>>> 'N' in 'STACK OVERFLOW'
False
>>> 'N' in 'python' # uppercase and lowercase are not equal
False
>>> 'N' in 'python'.upper()
True

Also, there is no need for a conditional statement when assigning to your flag. Rather than

flag = False
if 'N' in your_string:
   flag = True

do

flag = 'N' in your_string
like image 101
Steven Rumbalski Avatar answered Oct 08 '22 14:10

Steven Rumbalski