But how do you get at more than one character

For Starbuzz, you don't just need a single character. You need to extract the price from the string of HTML, and the price is made up of several characters.

You need to extract a smaller substring from a bigger string. A substring is a sequence of characters contained within another string. Specifying substrings in Python is a little like reading single characters from a string, except that you provide two index values within the square brackets:

Ii v,ou provide a si^le mde* afk W-'aUe^r* yt a s^le matter-

s[138:147]

IS Vou provide -tvlo i/de* values, you «¿«it a II .Karats W -tKe -first „de, u? fc Lt «ot ti* sedond mde*.

Let's work out what each of the following substring specifications mean. Imagine the variable s is set to the string below. Your job is to determine what each of the substrings provide.

In general, if you specify a substring using s[a:b], then: a is b is ...

here is waldo

Imagine the variable s is set to the string below. Your job was to determine what each of the substrings provide.

12 minus 10 = 2 characters s[13:18]

Si 10 minus \l - 5 characters

In general, if you specify a substring using s [a:b], then: a is the index of the first character b is the index after the last character

WP "to, but not including.

12 minus 10 = 2 characters s[13:18]

Si 10 minus \l - 5 characters

In general, if you specify a substring using s [a:b], then: a is the index of the first character b is the index after the last character

WP "to, but not including.

[xjl^lJMSUJIaJloJUJ

"C.£Ven though index 18 is mentioned in the substring speCifiCation, it's not included in the extracted substring.

The second index value is after the last character in the substring

This is even though the first index value is the start character of the substring.

You need to update the program to extract the price starting at the 235th character of the string. The price is four characters long. Store the price substring in a variabl e called price. Write the new version of the program here:

yfctte the »ew tode here^

extract the price

You needed to update the program to extract the price starting at the 235th character of the string. The price is four characters long and stored in a variable called price.

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

print(pride)

price = textC234:2383

Now, instead of printing, "text" (to e^ire web page), you are just printing pvCe <-the extracted substring)-

The i dha^s ai

w.th two mdex values, separated by the V

This is the first character m the required substring, at index

Here it <s: the substring to extract and the Current price of Coffee-

Was this article helpful?

0 0

Post a comment