0

This is a very simple function in Python

def not_string(str1):
    if str1[0:3] is "not":
        return str1
    else:
        return 'not ' + str1

str1 = 'not bad'
print(not_string(str1))

The output of the above code should be "not bad" but the Python is showing output as "not not bad. Can anyone explain what I am missing here?

1 Answer 1

2

The is operator in Python tests for object equality rather than value equality. What you want is for your if statement to be if str1[0:3] == "not":

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

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.