如何确定子字符串是否在其他字符串中

问题:如何确定子字符串是否在其他字符串中

我有一个子字符串:

substring = "please help me out"

我还有另一个字符串:

string = "please help me out so that I could solve this"

如何查找是否substringstring使用Python 的子集?

I have a sub-string:

substring = "please help me out"

I have another string:

string = "please help me out so that I could solve this"

How do I find if substring is a subset of string using Python?


回答 0

insubstring in string

>>> substring = "please help me out"
>>> string = "please help me out so that I could solve this"
>>> substring in string
True

with in: substring in string:

>>> substring = "please help me out"
>>> string = "please help me out so that I could solve this"
>>> substring in string
True

回答 1

foo = "blahblahblah"
bar = "somethingblahblahblahmeep"
if foo in bar:
    # do something

(顺便说一句-尝试不命名变量string,因为存在一个具有相同名称的Python标准库。如果在大型项目中这样做,可能会使人们感到困惑,因此避免这种冲突是一种很好的习惯。)

foo = "blahblahblah"
bar = "somethingblahblahblahmeep"
if foo in bar:
    # do something

(By the way – try to not name a variable string, since there’s a Python standard library with the same name. You might confuse people if you do that in a large project, so avoiding collisions like that is a good habit to get into.)


回答 2

如果您要寻找的不是True / False,那么最适合使用re模块,例如:

import re
search="please help me out"
fullstring="please help me out so that I could solve this"
s = re.search(search,fullstring)
print(s.group())

s.group() 将返回字符串“请帮助我”。

If you’re looking for more than a True/False, you’d be best suited to use the re module, like:

import re
search="please help me out"
fullstring="please help me out so that I could solve this"
s = re.search(search,fullstring)
print(s.group())

s.group() will return the string “please help me out”.


回答 3

我想我会在您不希望使用Python的内置函数inor的情况下进行技术面试时添加此内容find,这很可怕,但是确实发生了:

string = "Samantha"
word = "man"

def find_sub_string(word, string):
  len_word = len(word)  #returns 3

  for i in range(len(string)-1):
    if string[i: i + len_word] == word:
  return True

  else:
    return False

Thought I would add this in case you are looking at how to do this for a technical interview where they don’t want you to use Python’s built-in function in or find, which is horrible, but does happen:

string = "Samantha"
word = "man"

def find_sub_string(word, string):
  len_word = len(word)  #returns 3

  for i in range(len(string)-1):
    if string[i: i + len_word] == word:
  return True

  else:
    return False

回答 4

人们提到了string.find()string.index(),并string.indexOf()在评论中进行了总结(根据Python文档):

首先没有string.indexOf()方法。Deviljho发布的链接显示这是一个JavaScript函数。

其次,string.find()string.index()实际上返回子字符串的索引。唯一的区别是它们如何处理子字符串没有发现的情况:string.find()回报-1string.index()引发ValueError

People mentioned string.find(), string.index(), and string.indexOf() in the comments, and I summarize them here (according to the Python Documentation):

First of all there is not a string.indexOf() method. The link posted by Deviljho shows this is a JavaScript function.

Second the string.find() and string.index() actually return the index of a substring. The only difference is how they handle the substring not found situation: string.find() returns -1 while string.index() raises an ValueError.


回答 5

您也可以尝试find()方法。它确定字符串str是出现在字符串中还是出现在字符串的子字符串中。

str1 = "please help me out so that I could solve this"
str2 = "please help me out"

if (str1.find(str2)>=0):
  print("True")
else:
  print ("False")

You can also try find() method. It determines if string str occurs in string, or in a substring of string.

str1 = "please help me out so that I could solve this"
str2 = "please help me out"

if (str1.find(str2)>=0):
  print("True")
else:
  print ("False")

回答 6

In [7]: substring = "please help me out"

In [8]: string = "please help me out so that I could solve this"

In [9]: substring in string
Out[9]: True
In [7]: substring = "please help me out"

In [8]: string = "please help me out so that I could solve this"

In [9]: substring in string
Out[9]: True

回答 7

def find_substring():
    s = 'bobobnnnnbobmmmbosssbob'
    cnt = 0
    for i in range(len(s)):
        if s[i:i+3] == 'bob':
            cnt += 1
    print 'bob found: ' + str(cnt)
    return cnt

def main():
    print(find_substring())

main()
def find_substring():
    s = 'bobobnnnnbobmmmbosssbob'
    cnt = 0
    for i in range(len(s)):
        if s[i:i+3] == 'bob':
            cnt += 1
    print 'bob found: ' + str(cnt)
    return cnt

def main():
    print(find_substring())

main()

回答 8

也可以用这种方法

if substring in string:
    print(string + '\n Yes located at:'.format(string.find(substring)))

Can also use this method

if substring in string:
    print(string + '\n Yes located at:'.format(string.find(substring)))

回答 9

除了使用find()之外,一种简单的方法是如上所述使用’in’。

如果’str’中存在’substring’,则将执行part,否则执行part。

Instead Of using find(), One of the easy way is the Use of ‘in’ as above.

if ‘substring’ is present in ‘str’ then if part will execute otherwise else part will execute.