xxxxxxxxxx
const string = "mainstring";
const substring = "string";
console.log(string.includes(substring)); //true
xxxxxxxxxx
>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'
In Python, substrings are a sequence of characters or character(string of length 1) within another string. We can find out if a substring belongs to a string using the find() method and in operator.
xxxxxxxxxx
learn_coding = "You can learn to code for free! Yes, for free!"
substring = "paid"
print(learn_coding.index(substring))
# output
# Traceback (most recent call last):
# File "main.py", line 4, in <module>
# print(learn_coding.index(substring))
# ValueError: substring not found