7

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.

3
  • 2
    "N" in mystring ? Commented Jul 18, 2016 at 18:05
  • 1
    'N' in 'CNDDDNTD' returns True, 'N' in 'CCGGTTT' returns False Commented Jul 18, 2016 at 18:05
  • You can do flag = "n" in string or "N" in string in just one line. Commented Aug 8, 2023 at 14:03

1 Answer 1

13
>>> '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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.