Python Forum

Full Version: Substrings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The task is to find a the first string as a substring in the other string.
For example, let's say we have:
string 1 = 'bob'
string 2 = 'bobsbugsbegone'
for i in s2:
s1 == to [b]ONLY[/b] [0:2] of s2

I want to write a code that detects when there's a substring in a string and that counts how many substrings are found. (the second part is easy)

This is my code:

import sys

s1 = sys.argv[1]
s2 = sys.argv[2]
k = 0
i = 0

def substrings(s1, s2, k):
	for k in s2:
		for n in range(len(s2)):
			if k == s1[n]:
				return True
				print('s1 is a substring in s2')
			else:
				print('s1 is not a substring in s2')
				return False
		
print(substrings(s1, s2, k))
Help?

In addition to that:
Error:
(C:\Users\OmarHS\Miniconda3) c:\Users\OmarHS\Desktop\AUB\CMPS\200\ASST3>python substrings.py bob bobsbugsbegone True (C:\Users\OmarHS\Miniconda3) c:\Users\OmarHS\Desktop\AUB\CMPS\200\ASST3>python substrings.py bob robsbugsbegone s1 is not a substring in s2 False (C:\Users\OmarHS\Miniconda3) c:\Users\OmarHS\Desktop\AUB\CMPS\200\ASST3>python substrings.py bob robsbobsbegone s1 is not a substring in s2 False
In the first try it works
In the second try it also works
In the third try, i found out that it works only if the substring is at the beginning of the second string
Quote:I want to write a code that detects when there's a substring in a string and that counts how many substrings are found.
you mean like this?
Output:
>>> import re >>> [m.start() for m in re.finditer('test', 'test test test test')] [0, 5, 10, 15]
The length of list is the amount of occurrences, The list elements, are the string position they were found in.
Why don't you use s.find? The .find() function returns the index of the string the substring starts at, so for example:
"jgjbobjdeh".find("bob")
returns "3", because the substring "bob" starts at index 3. If there are multiple substrings in the string, find() will get the first one:
>>> "hfaibobgjdbobjfdks".find("bob")
4
You could easily make a function that turns the indexes each substring starts at into a list by using find and then removing the characters before the substring starts with string slicing and then search again. I'm currently in a lazy state so I'll let you make the script.

EDIT: Metalburr beat me to it and also used a much simpler and compact method haha.
Okay, so the instructions are actually:
Write a function substring(s1,s2,k) that takes two strings and an integer index k, and returns True if the first string appears as a substring in the second at location k, and False otherwise.
Write a function how_many() that takes two strings and returns the number of times the first string ocurs in the second.


My NEW code satisfies the first, not the second:
import sys

s1 = sys.argv[1]
s2= sys.argv[2]
k = 0

def substring(s1, s2, k):
	k = s2.find(s1)
	return k

print(substring(s1,s2,k))
(Sep-26-2017, 11:52 AM)OmarSinno Wrote: [ -> ]My NEW code satisfies the first, not the second:

No it doesn't.  You ignore the k parameter, so your function will return a Truthy value when it shouldn't.
Also, the definition says you should return True/False, but right now you're returning an index that the substring starts at.  Hint: str.find() returns -1 if the substring is not found.