mardi 24 février 2015

Python, find a word and the location of each character in that word in a string

Let's say in Python I have this sentence:



my_sentence = "I like to drive my car around the round block twice daily"


And I want to find the word 'round' in this sentence. I know I can use find, index, rfind, and rindexlike so:



print my_sentence.find('round')
>>>24


What I want to do is find the location of each letter in 'round' within the sentence and only use the first occurrence of the word 'round'. The most important letter to find is the last letter and have it print the location of the last letter of the first 'round' with each other letter and stop. I've tried to do this:



last_letter = my_sentence.find('round')
last_letter2 = last_letter[-1::]


This produces an error. So, what's the best way of getting the location of each letter of a word that exists in a string? I'm new to Python and I saw there was ways to find where a word was, but not each letter of the word in the string. How do I do this?


Aucun commentaire:

Enregistrer un commentaire