Stackstack Stackstack - 1 month ago 7
Python Question

Find function in Python 3.x

When we have the following:

tweet2 = 'Want cheap snacks? Visit @cssu office in BA2283'


print(tweet2[tweet2.find('cheap')])
results in the output
'c'
and I cant wrap my head around how it does this. I tried the visualizer and it didn't show anything. Could anyone please explain?

Answer

tweet2.find('cheap') returns the index at which the beginning of "cheap" is found, and when that index is used in tweet2[index], it returns the character at that index, which is "c"

Comments