Stack Exchange
printing specific characters that differ at same spot in two strings
I am trying to write code that prints out pairs of letters, one from the same place in each string, when the character in either one of the strings is "-" or "?".
ref_seq = "AGGTCATCAGGGAAA??TCTAGAACCC"
map_seq = "AGGTCTTCAAAAAAAGG---G"

#defining which sequence is longer/shorter
longest_seq = map_seq 
shortest_seq = ref_seq
if len(ref_seq) > len(map_seq):
    longest_seq == ref_seq
    shortest_seq == map_seq
#adding on characters to shortest sequence to make sequences same length
x = len(longest_seq) - len(shortest_seq)
shortest_seq += ("$" * x)

#printing out sites with gaps or unknown bases
print "sites with gaps or unknown bases"
for i in range(len(longest_seq)):   
    if longest_seq[i] == "-" or "?":
        print (i+1), longest_seq[i], shortest_seq[i]
    elif shortest_seq[i] == "-" or "?":
        print (i+1), longest_seq[i], shortest_seq[i]

My code is printing out all the sites, not just where the sites are either "?" or "-". Can someone explain how I can edit my code so that only the sites with "?" or "-" are printed? 
The first block of code works fine, but I am including to explain the variables I am using, the problems start after #printing out sites with gaps or unknown bases.
I'm a beginner so an explanation would really help me improve. I think it may be something to do with the "if" elif" in my for loop, but I am not sure.

Your main problem is with your if/elif block as you assumed! The issue is that you are checking if longest_seq[i] == "-" or "?" instead of if longest_seq[i] == "-" or longest_seq[i] == "?". Just saying if "?" evaluates to if True because strings are not null. This will need to be repeated for the longest_seq also. BONUS!: A way to shorten this is to say if longest_seq[i] in ("-",  "?")

@TheLazyScripter Thanks for the detailed response and the shortened method. Can you explain what you mean by "strings are not null"? I don't think my boolean logic is very good :/

What i mean is that they are either truthy or falsey. truthy/falsey are terms that refer to an object that will either satisfy an if var: or while var but are different than True and False. A list of items that are falsey are as follows. (True, any_digit_but_zero, any_decimal_but_zero, (any_val), [any_val], {any:any}, set(any), "any", 'any')'. Each of these items will satisfy a if any_item_above`. It's a hard concept to explain in this few chars! But I hope this helped. A Good Article

What I still do not understand is they way you deal with the different lengths. When I run your code, x is -6 and none of both strings is concatenated by dollars, but there's no check for dollarsigns at the end anyway, so... How should the rest of the longer string be treated?

I assume he is only adding $ as a default way to match string lengths to avoid any IndexError. They would just be passed over. It would be best to error check, but that may be beyond the scope of OP's current skills.

@thelazyscripter yes, might be the reason. But by using zip everything would be done exactly like that automatically, without error checking or the need to fill any length gaps.

@SpghttCd @TheLazyScripter had it right, I'm just using $ for matching string lengths. I know it can't be the best method but I'm only learning!

Of course you do, that's why we are here. As I mentioned: have a look at zip - it provides the possibility to iterate ober two (or more) iterables in parallel. And if they have have different lengths, it automatically stops when the shortest one reached its end. Imo exactly what you need here. So the code for the complete task reduces to the for loop I showed you in my answer.

Your problem is simply lacking parantheses in your if command, together with misconception of the or operator, because or "?" is always True, no matter what's on the left handside.
To be clear, your command should look like
if (longest_seq[i] == "-") or (longest_seq[i] == "?"):


However, I think you can still make some improvements.
One of the most obvious things is: don't do for x in range(len(whatever)) in python. You have enumerate for this. Try it out - you'll love it.
Another useful thing is zip and the in operator, so your code would imo be better written like
ref_seq = "AGGTCATCAGGGAAA??TCTAGAACCC"
map_seq = "AGGTCTTCAAAAAAAGG---G"

for i, (r, m) in enumerate(zip(ref_seq, map_seq)):   
    if (r in "-?") or (m in "-?"):
        print (i+1), r, m


where do the brackets go? I'm sorry I'm a beginner.

the parenthesis are not necessary

can you explain why or "?"  is always true?

Because you can use other datatypes than bool for rather meaningful decisions, so they can be translated to boolean values according to some simple and useful rules. So integers e.g. are False when 0, True for the rest. Accordingly for strings: False for the empty string, True for the rest - and so is "?".

Yup. Spghtti has the clearest answer, as a point of illustration you can also look at the image I provided in my answer below.
3== '-' or '?' evalutes to '?' but still passes through the if statement.

i think you mean print(i+1, r, m)

@Chris_Rands no, I didn't. Because OP was using Python2 syntax, I chose to do the same. But for Python3 you would be right of course.

Notice that in your code, the if and elif statements are labelled as 
longest_seq[i] == "-" or "?". What you need to do is evaluate each statement individually, like the below code.
    if (longest_seq[i] == "-") or (longest_seq[i] == "?"):
        print ((i+1), longest_seq[i], shortest_seq[i])
    elif (longest_seq[i] == "-") or (longest_seq[i] == "?"):
        print ((i+1), longest_seq[i], shortest_seq[i])

As to why this happens, take a look at this fun illustration below:

The 3 == '-' is evaluated first, to FALSE, but when combined with the or '?' evaluates to '?'. The '?' value is not 'false' so the if statement goes YOLO and decides to proceed anyway. 

There is actually no need to wrap the statement in a ()

Ah, true. I usually just do it out of habit and for clarity.

It's good habit, especially if you are evaluating multiple statements.

I left the parenthesis on, but I changed my phrasing to indicate that each statement should be evaluated individually instead. Thanks for pointing it out!

@Enthus3d I think I get it now, thanks for the picture!

@wilberox no problem :). I didn't really help you solve the problem, but I'm glad the illustration helps :P

Change the if statement:
for i in range(len(longest_seq)):
    print(longest_seq[i])
    if longest_seq[i] == "-" or longest_seq[i] == "?":
        print ((i+1), longest_seq[i], shortest_seq[i])
    elif shortest_seq[i] == "-" or shortest_seq[i] == "?":
        print ((i+1), longest_seq[i], shortest_seq[i])

