2

I'm writing this function to detect if two strings are anagrams. I want to convert the strings into lower case characters in case one of the characters is in upper case, but what I wrote doesn't seem to be working properly.

# function to check if two strings areanagram or not
def eh_anagrama(cad1, cad2):
    if cad1.islower() == False:
        cad1.lower()
    if cad2.islower() == False:
        cad2.lower()
    if(sorted(cad1)== sorted(cad2)):
        print("The strings are anagrams.")
    else:
        print("The strings aren't anagrams.")
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
João
  • 73
  • 4
  • 1
    Please give a [mre], _"doesn't seem to be working properly"_ isn't much help. – jonrsharpe Oct 15 '21 at 18:39
  • Does this answer your question? [How do I lowercase a string in Python?](https://stackoverflow.com/questions/6797984/how-do-i-lowercase-a-string-in-python) – MatBBastos Oct 15 '21 at 19:02

4 Answers4

1

You don't need to check if they're lower, as they would be compared in lower case anyway:

def eh_anagrama(cad1, cad2):
    if sorted(cad1.lower()) == sorted(cad2.lower()):
        print("The strings are anagrams.")
    else:
        print("The strings aren't anagrams.")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
scandav
  • 749
  • 1
  • 7
  • 21
0

Calling cad1.lower() will convert this string to lowercase, but this value isn't used, you could try cad1 = cad1.lower()

Edit: Also, it's more "pythonic" using if not cad1.islower() as opposed to if cad1.islower() == False

UdonN00dle
  • 723
  • 6
  • 28
  • 1
    On "more Pythonic" not to compare to `False`, that's true (PEP 8 specifically discourages it), but in this particular case, there's hardly any reason to even perform the test; saving a perhaps unnecessary string allocation at the expense of scanning the string twice when you need to lowercase it anyway is pretty pointless. – ShadowRanger Oct 15 '21 at 18:44
  • 1
    okok, thanks for the tip, I'm still a bit of a noob, learning step by step! – João Oct 15 '21 at 18:45
  • @ShadowRanger this comment should probably be in your own answer – norok2 Oct 15 '21 at 19:34
0

Just unconditionally convert, and reassign the lowercased string (str are immutable, so methods return new strings, they don't change the one they're called on):

def eh_anagrama(cad1, cad2):
    cad1 = cad1.lower()  # Reassign to replace with lowercased string
    cad2 = cad2.lower()  # Ditto
    if sorted(cad1) == sorted(cad2):
        print("The strings are anagrams.")
    else:
        print("The strings aren't anagrams.")

Minor side-note: For handling non-English alphabets more correctly, I'd suggest using .casefold() instead of .lower(); in other languages, this can make a meaningful difference.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

I made some changes. Instead of just writing cad1.lower() or cad2.lower() I assigned them to the variable corresponding to the value.

cad1 = cad1.lower() and cad2 = cad2.lower()

# function to check if two strings areanagram or not
def eh_anagrama(cad1, cad2):

    cad1 = cad1.lower() ; cad2 = cad2.lower()

    if sorted(cad1) == sorted(cad2):
        print("The strings are anagrams.")
    else:
        print("The strings aren't anagrams.")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
SeekNDstroy
  • 302
  • 2
  • 6