“Python finden in String” Code-Antworten

Finden Sie String in String Python

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

Python Wie man in der String sucht

search_text = 'man'

str = 'You are good man.'

if search_text in str:
    print(f'Found "{search_text}" in string')
else:
    print('Not found')
MrStonkus

Python String finden

The find() method returns the index of first occurrence of the substring
(if found). If not found, it returns -1.
message = 'Python is a fun programming language'

# check the index of 'fun'
print(message.find('fun'))

# Output: 12
Motionless Manatee

Python finden in String

Input:

myStr = "py py py py"
print(myStr.find("js"))
print(myStr.index("js"))


Output:

-1
Traceback (most recent call last):
  File "D:\Programming\python\article\find.py", line 38, in <module>
    print(myStr.index("js"))
ValueError: substring not found
codelearner

Ähnliche Antworten wie “Python finden in String”

Fragen ähnlich wie “Python finden in String”

Weitere verwandte Antworten zu “Python finden in String” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen