Decode School

Decode School - The School for Problem Solvers
Welcome to My Blog.
Need Assignment / Project Guidance (or) Training in Python?

find substring in string pyhton

python program to get a String and Substring and find where the substring is present in String.

Sample Input 1 :

entertainment ain

Sample Output 1 :

6

Program or Solution

					    
							
s1=input("Enter a String:")
s2=input("Enter Substring:")
count=0
for i in range(0,len(s1)-len(s2)+1):
    j=0
    b=0
    if(s1[i]==s2[j]):
        k=i
        while(j<len(s2)):
            if(s1[k]!=s2[j]):
                b=1
                break
            j=j+1
            k=k+1
        if(b==0):
            print("The Sub string is at {}".format(i))
            break
else:
    print("Sub String Not found")

            
            
				

					    
					

Program Explanation

check whether first character of sub string is equal to any character in string.

If it is equal, then check remaining characters are equal to the characters in substring.

else move to next character.