Currently, your code will always return True.
for i in range(0, len(str1), 1):
this will iterate over the range of the length of str1, if the string is "python" The for loop will have these values for 'i': 0, 1, 2, 3, 4, 5 If you want to iterate over every single letter write
for i in str1:
This will make the values of i: p, y, t, h, o, n
Alternatively, if you use a range, you can check the individual letters with str1[i]. This will output the following: str1[0] == "p", str1[1] == "y" etc.
Since you do the same in your if statement, it will check if 'i' from range(0, 6) is equal to i. The first value of 'i' will be 0, after this first check it pass the if statement and will return True, meaning it will end the loop. This means it will only check this first case.
What you want to do is check over every letter in str1, if it is anywhere in str2, remove that instance from str2 and check the next letter. If at any time the letter is NOT in str2, return False. After checking all the letters and you didn't return False, return True.
Since strings are immutable, you can put them in a list first, and iterate over the list.
Check out the following code:
def check_anagram(str1, str2):
if len(str1) != len(str2):
return False
else:
string1 = [x for x in str1] # put str1 in list string1
string2 = [x for x in str2]
for i in range(0, len(str2), 1): # iterate over the range (length) of str2
if string1[i] in string2: # if letter in position i of string1 occurs in string 2 then:
string2.remove(string1[i]) # remove that letter from the string2 list
else:
return False # if it does NOT occur in string2, it is NOT an anagram, so return false
return True # If the loop went correctly, return true
edit:
If you want to remove whitespace (spaces) (since "ars magna" is an anagram of "anagrams", but the program won't pick it up since the length differs) you have to do this at the start. Replacing whitespace in a string can be done by the .replace(WHAT TO REPLACE, REPLACE WITH THIS) function. Place this right at the beginning of the check_anagram function:
str1 = str1.replace(" ", "")
str2 = str2.replace(" ", "")