“Ciąg Python zawiera” Kod odpowiedzi

String Python zawiera podłoże

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print "Found!"
else:
    print "Not found!"
riffrazor

Python Str zawiera słowo

fullstring = "StackAbuse"
substring = "tack"

if substring in fullstring:
    print "Found!"
else:
    print "Not found!"
Comfortable Cottonmouth

Ciąg Python zawiera

>>> str = "Messi is the best soccer player"
>>> "soccer" in str
True
>>> "football" in str
False
Misty Macaw

Jeśli nie podłoże w Pythonie String

>>> string = "Hello World"
>>> # Check Sub-String in String
>>> "World" in string
True
>>> # Check Sub-String not in String
>>> "World" not in string
False
peamdev

Jak sprawdzić podłoże w Pythonie

def find_string(string,sub_string):
	return string.find(sub_string)
#.find() also accounts for multiple occurence of the substring in the given string
san_bt

Python, jeśli ciąg zawiera char

myString = "<text contains this>"
myOtherString = "AnotherString"

# Casting to string is not needed but it's good practice
# to check for errors 

if str(myString) in str(myOtherString): 
    # Do Something
else:
	# myOtherString didn't contain myString
    
Av3

Odpowiedzi podobne do “Ciąg Python zawiera”

Pytania podobne do “Ciąg Python zawiera”

Więcej pokrewnych odpowiedzi na “Ciąg Python zawiera” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu