In Python, a string is a sequence of characters or text that is enclosed with either single quotes (' '
) or double quotes (" "
).
For example, 'Theo'
is a string. Whether the characters are enclosed in single quotes or double quotes, it is still the same string; in other words, 'Theo'
is the same as "Theo"
.
in
keywordThe in
statement is used to check if a character or sequence of characters is present in a string.
The return value of the in
keyword is either True
or False
.
# creating a stringaxiom = 'Nothing worth while ever is'# using the in keywordprint('never' in axiom)print('ever' in axiom)
axiom
.never
is present in the string we created.ever
is present in the string we created.The in
keyword can also be used in an if
statement.
# creating a stringaxiom = 'Nothing worth while ever is'# using the if statementif 'ever' in axiom:print("True! The word 'ever' is present in the string.")
In the code above, we use the in
keyword to check if the word 'ever'
is present in the string we created, and we get True
as the result.