Python find

If you need to know where in a string of text your search term was found then you can use the find() function in Python. For example, previously, you were asked to use slicing to turn Kenny Carney into Carney Kenny. The solution involved hard-coded values:

surname = myName[6:]
firstName = myName[:5]
print(surname + " " + firstName)

Suppose now we asked you turn any first name and surname combination into a surname and first name answer. So, if you were called Jessica Jones and entered that at the prompt, hitting the enter key on your keyboard would turn it into Jones Jessica. Or if you were called Luke Cage, the output window would print Cage Luke. How would you go about it? You can't use hard-coded values for your slicing anymore.

One solution is to locate the position of the space in the entered name. Once you know the position of the space, you can use that information in your solution. Let's see how it's done.

The find() function looks like this:

yourString.find("your_text")

So you type the variable name that contains the text you want to search. After a dot, type find. The find function needs at least one thing between round brackets: what you are searching for. The find function returns a value. So you need to place it on the right of an equal sign:

returnValue = yourString.find("your_text")

If your text is found in your string then find will return where it was found, as a position number. (It will only find the first occurrence of your text.) Set up the following variable to try it out:

myName = "Jessica Jones"
returnValue = myName.find(" ")
print(returnValue)

In between the round brackets of find, we have typed two double quotes with a space between the two. (You can use single quotes, if you prefer.) This means find will return the first occurrence of a space in the name Jessica Jones. The position on the space will be placed in the variable we called returnValue. If find can't locate a space, it will return a value of -1. You can test for a value of -1 in an if Statement:

if returnValue != -1:

print("Search term found")

else:

print("Search term NOT found!")

We're using the != symbols to say "If returnValue does not equal -1". If it doesn't then our search term was found.

If our search term was found then we can go ahead and slice up the name:

myName = "Jessica Jones"
returnValue = myName.find(' ')

if returnValue != -1:

fName = myName[:returnValue]
sName = myName[returnValue + 1:]
print(sName + " " + fName)

else:

print("Error")

Notice how we are doing the slicing.

fName = myName[:returnValue]
sName = myName[returnValue + 1:]

The position of the space is 7. This is the start of the space. The first name can be grabbed using 0:7, if we were hard-coding it. But the find function has produced a number for us, so we can just use the returnValue variable as the final number the slice:

myName[:returnValue]

We don't need the zero as the first number. Remember: if you miss out the start number for your slice, Python will start slicing at the first character.

The surname begins one character after the space. Python allows you to add numbers to your index numbers. We're adding 1 to whatever is inside of returnValue. After a colon, we don't need a number because Python will just grab all the characters to the end of the string.

Finally, we can print out the results:

print(sName + " " + fName)

We use concatenation to join the surname with a space character then the first name.

Try it out with an input line at the start of your code:

myName = input("What is your name?")

Now delete the Jessica Jones line. Here's what your code should look like:

myName = input("What is your name?")
returnValue = myName. find(" ")

if returnValue != -1:

fName = myName[:returnValue]
sName = myName[returnValue + 1:]
print(sName + " " + fName)

else:

print("Error")

Run your code and enter Luke Cage when you see the prompt in the output window. Press the enter key on your keyboard and you should see Cage Luke print out.

Using the find function along with slicing can be very powerful. In the next lesson, you'll learn how to use len and count in Python. It's only a short lesson.

Python len and count functions >