The return value was the index of w which in this case was the integer 6. I stored the result of the operation in a variable named search_fave_phrase and then printed its contents to the console. I called the find() method on the variable containing the string and searched for the letter 'w' inside Hello world!. I created a variable named fave_phrase and stored the string Hello world!. Search_fave_phrase = fave_phrase.find("w") You can take a single word and search to find the index number of a specific letter: fave_phrase = "Hello world!" The following examples illustrate how to use the find() method using the only required parameter – the substring you want to search. How to Use find() with No Start and End Parameters Example If the substring you are searching for is not present in the string, then find() will return -1. If the substring is present in the string, find() returns the index, or the character position, of the first occurrence of the specified substring from that given string. The return value of the find() method is an integer value. Both the start_index_number and the end_index_number specify the range over which the search will take place and they narrow the search down to a particular section.It specifies the end index and where the search will stop. end_index_number is the third parameter and it's also optional.It specifies the starting index and the position from which the search will start. start_index_number is the second parameter and it's optional. This is the substring you are trying to find inside string_object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |