Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Slicing Off the Text After the Info

00:00 I found where Dionysus starts now I need to figure out where it ends. And I said I’m going to do that by first identifying where the next opening angular bracket is, which means I’ll need to save just this text because otherwise if I start from the whole html_text at the beginning, it’ll just find the first opening angular bracket, which doesn’t help me much.

00:21 However, I can start searching at a certain index. So I could say, let’s save this one here.

00:31 info_start_index is

00:39 start_index plus the length of the search string. Yeah, that’s right. That’s my info_start_index. And now I want to find the next, open the index of the next opening angular brackets, html_text.find().

00:54 And I want to find an opening bracket. And I want to start at the info_start_index so this is where I want to start searching. See what I’ll get there. So I’ll run this and that looks pretty good I get 155.

01:15 Now if I slice until there, that would be the end and or info_end_index.

01:29 Let’s see if that works. And then I want to slice html_text from the info_start_index up to, but not including the info_end_index.

01:43 Okay? Alright we’re going to print it out too to see anything.

01:52 So I pass this as an argument to print() function and then run the script again. And here we are I got Dionysus. All right, it looks good. There’s not any whitespace it seems that I need to deal with in this case, but still want to add that because it’s also quite quick.

02:09 But first let’s save that info

02:15 and then remove the whitespace by calling .strip() on info. And by default, if I don’t pass anything in there, it removes all the common whitespace characters so I can now print out info.strip() and I get the Dionysus.

02:35 Okay, so that would be the first step. I get the name, I should be able to use this same for the favorite color. Alright, so I could go ahead and copy paste this, but that’s of course not a great approach.

02:50 Instead, in the next lesson, I’m going to clean this up a bit, maybe rethink some of the variable naming and make sure that I can use it for more than just the name by generalizing it search string a little bit and probably wrapping it inside of a for loop.

Become a Member to join the conversation.