Psst! Check what courses we have in our offer, and get them all here to save $727. Act fast–this deal is available only 3/14–3/21.
Introduction
Basic utility functions
3. The index() and find() functions
Basic manipulation functions
Replacing and splitting
Summary

Instruction

Good job! Next, we have two similar functions, index() and find():

text = ('In this course, we will learn a lot of things related to Python.'
  'It is an amazing modern programming language that we highly recommend.'
  'We are going to cover variables, loops, conditional statements and lists.')
print(text.index('loops'))
print(text.find('loops'))

Both index('loops') and find('loops') search for the 'loops' string in the text variable. If found, both of them return the index of the first occurrence of 'loops'. The code above will print the 167 index twice.

When the substring is not found in the text, there is a subtle difference between these two functions: find() returns -1, whereas index() raises a ValueError.

If you'd like to learn how to handle exceptions in Python, take a look at Python Basics Part 2.

Exercise

E-mail addresses have the following format:

local-part@domain

Ask the user for their e-mail address:

Please provide your e-mail address:

If the address does not contain the @ sign, print:

Not a correct e-mail address.

Otherwise, print:

The local part is: {local-part}
The domain is: {domain}

Stuck? Here's a hint!

Check the index of the @ sign. The local-part is everything with indices lower than the index of @. The domain is everything with indices greater than the index of @.