Python data is smart

The more code you write, the more you will find that you need to do the same kind of things to the data in your variables all the time. To prevent you from having to create the same code over and over, programming languages provide built-in functionality to help you avoid writing unnecessary code. Python data is smart: it can do things.

Let's look at an example.

Imagine you have a piece of text in a variable that you want to display in uppercase (all CAPITAL letters):

msg = "Monster truck rally. 4pm. Monday."

You could write code that read through each character in the string and printed out the matching uppercase letter. But if you're programming in a language like Python, you can do this:

The dot means w print(msg.upper())

tteve's what gets ____displayed, the value of

MONSTER TRUCK RALLY. 4PM. MONDAY.

the msg vanable in UPPERCASE.

But what does msg.upper() mean?

Well, msg is the string containing our piece of text. The . upper () that follows it is called a string method. A method is just an instruction for the string. When you call msg . upper (), you are telling the string to give you an UPPERCASE version of its data.

But is there a string method that can help you search for a substring within a string object?

These are some of the many built-in string methods that come with Python. Match each method to what it does. We've done one for you already.

Method text.endswith("".jpg")

What the method does

Return a copy of the string with all occurrences of one substring replaced by another.

Return a copy of the string converted to lowercase.

text.replace("tomorrow", "Tuesday"):

textfind("python"):

text.startswith("<HTML>")

Return the value True if the string has the given substring at the beginning.

Return the value True if the string has the given substring at the end.

Return the first index value when the given substring is found.

Return a copy of the string with the leading and trailing whitespace removed.

Return a copy of the string converted to uppercase.

Which of the above methods do you need to use to locate the price substring within the Beans'R'Us web page?

find, the right method

These are some of the many built-in string methods that come with Python. You were to match each method to what it does.

Method

What the method does

Return a copy of the string converted to uppercase.

Which of the above methods do you need to use to locate the price substring within the Beans'R'Us web page?

The "findO" method

You need to update your price-grabbing program so that it extracts the four-character substring that follows the occurence of the ">$" characters. Write the new version of your code in the space provided.

Hints: Don't forget that the find () method finds the starting position of a substring. Once you've found ">$", use Python's addition operator to calculate where in the string you want to extract the substring. The addition operator is the "+" symbol.

'H* really lockig coffee beans = <strong>$5.4 9</ strong></p><p>Price valid for finding the deal finding the deal

You needed to update your price-grabbing program so that it extracts the four-character substring that follows the occurence of the ">$" characters.

Hints: Don't forget that the find () method finds the starting position of a substring. Once you've found ">$", use Python's addition operator to calculate where in the string you want to extract the substring. The addition operator is the "+" symbol.

This tode hasn't ┬┐hanged.^

import urllibrequest page = urllibrequesturlopen(llhttp://wwwbeans-r-us.biz/prices.html")

This is the addition operator.

start_of_price = where + 2 The start of the actual price _______"""T)

is another 2 index positions .. .end_of_price .=. .star.t_of_pri.ce..+.. 4. _..

along the string, while the end f the price is another 4.

price = textCstart_of_price:end_of_priceJ

print(price)

Did you remember to print out the price once you'd found it?

With the start and end index locations known, it's easy to specify the substring required.

With the start and end index locations known, it's easy to specify the substring required.

Was this article helpful?

0 0

Post a comment