0

Im trying to write a function that get 2 arguments (2 strings actually) and compares them (ignoring the difference in upper/lower cases). For example:

cmr_func('House', 'HouSe')
true

cmr_func('Chair123', 'CHAIr123')
true

cmr_func('Mandy123', 'Mandy1234')
False.

Well, I tried something, but it seems very stupid and bad designed function, which anyway does not work. I would like to get idea. I believe i need to use some built-in str function, but im not sure how they can help me.

I thought about using in function with some loop. But i dont know on what kind of object should i apply a loop.

def str_comp(a,b):
    for i in a:
        i.lower()
    for i in b:
        i.lower()

    if a == b:
        print 'true'
    else:
        print 'false'

Any hint or idea are welcomed. Thanks :)

1
  • i.lower() doesn't do what you expect it to do in your code. It returns the string with all characters converted to lowercase, so calling just i.lower() doesn't do anything since you aren't using the value returned. Commented Dec 29, 2015 at 10:38

4 Answers 4

5

You can just convert both strings to lower-case and compare those results:

def str_comp (a, b):
    return a.lower() == b.lower()

The idea behind this is normalization. Basically, you want to take any input string, and normalize it in a way that all other strings that are considered equal result in the same normalized string. And then, you just need to compare the normalized strings for equality.

Other operations you might consider are stripping whitespace (using str.strip()), or even more complex operations like converting umlauts to 2-letter combinations (e.g. ä to ae).


The problem with your solution is that you seem to assume that iterating over a string will allow you to modify the characters individually. But strings are immutable, so you cannot modify an existing string without creating a new one. As such, when you iterate over a string using for i in a you get many individual, independent strings for each character which are in no way linked to the original string a. So modifying i will not affect a.

Similarly, just calling str.lower() will not modify the string either (since it’s immutable), so instead, the function will return a new string with all letters converted to lower-case.

Finally, you shouldn’t return a string “True” or “False”. Python has boolean constants True and False which should be used for that. And if you use them, you don’t need to do the following either:

if condition:
    return True
else:
    return False

Since condition already is interpreted as a boolean, you can just return the condition directly to get the same result:

return condition
Sign up to request clarification or add additional context in comments.

Comments

3

First you dont need to iterate the String to make all chars lowercase. You can just:

a.lower()
b.lower()

Or you can do it all together:

def str_comp(a,b):
    return a.lower() == b.lower()

Dont forget you're also returning True or False a Boolean, not returning a String (in this case the string "True" or "False")

If you want to return a String is function would different :

def str_comp(a,b):
    if a.lower() == b.lower()
        return "True"
    return "False"

2 Comments

Thank you all. I didnt think it would be that simple - i thought i have to define some kind of boolean variable first (though i dont know how or if its even exist in python). What is the difeerence between '=' and '==' ?
@AmirChazan = is an assignment of a value to a variable, while == is a comparison that returns a boolean value as a result.
3

The function str.lower() actually works in a slightly different way:

  1. This is no in-place modification. Calling a.lower() returns a copy of a with only lowercase letters and does not change a itself.
  2. str.lower() can be called on whole strings, not just characters, so the for i in a loop won't be necessary.

Therefore you could simplify your function like following:

def str_comp(a, b):
    if a.lower() == b.lower():
        print 'true'
    else:
        print 'false'

Comments

1
def are_strings_equal(string_1, string_2):
    return string_1.lower() == string_2.lower()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.