1
s1 = input("enter 1st string: ")
s2 = input("enter 2nd string: ")

if s2 in s1:
    print("found")
else:
    print("not found")

simply i want to say if i give some input to s1 and not give anything as input to s2 and press enter, Output of this code print "found" which is wrong because blank is not in my string s1, So why it is happening? and how to correct it?

2 Answers 2

2

Every string contains the empty string so '' in 'anything' is always True.

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

Comments

1

Every string always contains the empty string.

i = 'anything'.count('')

print(i)
# 9

As you can see, between every letter and before and after the string, there is 'the empty string'.

In your example, you can ensure that a non-empty string is entered like so:

while(True):
    s1 = input("enter 1st string: ")
    if s1 != '':
        break

while(True):
    s2 = input("enter 2nd string: ")
    if s2 != '':
        break

if s2 in s1:
    print("found")
else:
    print("not found")

Also note:

s  = '' + 'a' + '' + 'n'+ '' + 'y' + '' + 't' + '' + 'h' + '' + 'i' + '' + 'n' + '' + 'g' + ''

print(s == 'anything')
# True
print(s.count('')
# 9

Adding more empty strings does not have any effect.

s += ''
print(s.count('')
# 9

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.