I’m trying to compare a list of letters with letters that are in a text file, Everything works just fine except for the fact that even if the letter was equal to that of the text file; it’ll yield False
Here’s a sample of what’s happening:
with open("Comparing_file") as f: for line in f: line = line.strip("") # removing the spaces(if there was any) letter = "A" print("This is the line:", line) print("This is the letter:", letter) if letter.strip("") == line: # removing the spaces(if there was any) print("equal") else: print("not equal")
Here are the results:
This is the line: A This is the letter: A not equal
You are misusing strip.
S.strip([chars]) -> str Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead.
This means, that if you provide an argument to strip, it will remove whatever is in that argument. In your case, you provided
"" which is an empty string, meaning strip will basically do nothing.
Instead, you can try:
line.strip() letter.strip() # OR line.strip(" ") # this for example will remove spaces. line.strip("n") # this for example will remove "n".