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
  • 107,083
  • 22
  • 201
  • 376
João
  • 73
  • 4

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
  • 151,199
  • 17
  • 111
  • 216
scandav
  • 690
  • 7
  • 19
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
  • 652
  • 6
  • 26
  • 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
  • 124,179
  • 11
  • 158
  • 228
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
  • 151,199
  • 17
  • 111
  • 216
SeekNDstroy
  • 162
  • 6