Creating parts of strings

As we have seen before the find(string) function takes a target string as input and returns the index where that target string is found in the current string. If the string isn’t found it returns -1.

Note

The find function will return the first position it finds the given string in. Notice that above it printed 2 which means it found the “is” in “This” first.

Check your understanding

You can use the find function along with the slice feature to get part of a string. To get a slice (part) of a string use stringName[start:end], which returns a new string with all the characters from the start position to one before the end position.

Say that you are looking for a name in a string but don’t know the exact position of the name in the string. However you do know that it will be after name:.

You have attempted of activities on this page