如何确定子字符串是否在不同的字符串中

时间:2022-12-19 07:22:56

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?

如何使用Python查找substring是否是字符串的子集?

10 个解决方案

#1


165  

with in: substring in string:

with in:字符串中的substring:

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

#2


20  

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.)

(顺便说一句 - 尝试不命名变量字符串,因为有一个同名的Python标准库。如果你在一个大型项目中这样做,你可能会混淆人们,因此避免这样的冲突是一个很好的习惯。 )

#3


13  

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

如果您要寻找的不仅仅是真/假,那么您最适合使用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() will return the string "please help me out".

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

#4


9  

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:

以为我会添加这个,以防你正在考虑如何在技术面试中这样做,他们不希望你使用Python的内置函数或发现,这很可怕,但确实发生了:

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

#5


8  

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

人们在评论中提到了string.find(),string.index()和string.indexOf(),我在这里总结了它们(根据Python文档):

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

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

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.

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

#6


5  

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

您也可以尝试使用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")

#7


1  

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

#8


1  

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()

#9


0  

Can also use this method

也可以使用这种方法

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

#10


0  

如何确定子字符串是否在不同的字符串中

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

而不是使用find(),其中一个简单的方法是使用上面的'in'。

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

如果'str'中存在'substring',那么如果part将执行,否则part将执行。

#1


165  

with in: substring in string:

with in:字符串中的substring:

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

#2


20  

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.)

(顺便说一句 - 尝试不命名变量字符串,因为有一个同名的Python标准库。如果你在一个大型项目中这样做,你可能会混淆人们,因此避免这样的冲突是一个很好的习惯。 )

#3


13  

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

如果您要寻找的不仅仅是真/假,那么您最适合使用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() will return the string "please help me out".

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

#4


9  

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:

以为我会添加这个,以防你正在考虑如何在技术面试中这样做,他们不希望你使用Python的内置函数或发现,这很可怕,但确实发生了:

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

#5


8  

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

人们在评论中提到了string.find(),string.index()和string.indexOf(),我在这里总结了它们(根据Python文档):

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

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

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.

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

#6


5  

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

您也可以尝试使用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")

#7


1  

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

#8


1  

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()

#9


0  

Can also use this method

也可以使用这种方法

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

#10


0  

如何确定子字符串是否在不同的字符串中

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

而不是使用find(),其中一个简单的方法是使用上面的'in'。

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

如果'str'中存在'substring',那么如果part将执行,否则part将执行。