Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Substrings
#1
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
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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.
Reply
#4
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))
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help in substrings TDH 4 3,765 Oct-17-2017, 07:05 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020