W3docs

Check if a word is in a string in Python

You can use the in keyword to check if a word is in a string in Python.

You can use the in keyword to check if a word is in a string in Python. Here is an example code snippet:

Use the "in" keyword to check if a word is in a string

python— editable, runs on the server

This code will output:

output

'World' is in the string 'Hello, World!'

You can also use the str.find() method to check the word's index if present in the string. If the word is present it returns the starting index of the word otherwise it returns -1. Here is an example code snippet:

Use string find() method to check the word's index if present in the string

python— editable, runs on the server

This code will also output:

another output

'World' is in the string 'Hello, World!'