Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.Sign InEnroll

Python Basics

    Well done!

    You have completed Python Basics!

    Preview

    String Methods

    8:26withCraig Dennis

    Let's explore common string methods and discuss where to learn more

    Terms

    I realize the termobject might be a little confusing right now. Everything in Python is an object, so anything that gets created will be one of these objects. Objects can have data attributes, or properties, and methods, or behaviors.

    Methods are just functions owned by the object. We will look more at objects as well as how to create and define your own in upcoming courses.

    Let the word object roll, loosely translate to the way we use the word thing. This thing here...this object here.

    So again, don't worry if it's not completely clear yet, we'll get there!

    Related Discussions

    Have questions about this video? Start a discussion with the community and Treehouse staff.

    Sign up

      Related Discussions

      Have questions about this video? Start a discussion with the community and Treehouse staff.

      Sign up

      So let's start with a string variable.0:00
      It is a quote from Albert Einstein andit's a great one for0:03
      encouraging exploration here in the REPL.0:07
      So he said a person who never made0:10
      a mistake never tried anything new.0:14
      So there's a built in function thatlets you see the length of a string,0:21
      how many characters it contains.0:24
      It's name is len.0:26
      So we'll play len, and then we'll passin our object, our string object.0:28
      We'll play len(quote).0:32
      It has 58 characters, andthat includes spaces.0:34
      So len here, that's a function.0:37
      Remember that everythingyou create is an object.0:40
      So our string here that wecreated is an object, and0:43
      all objects can have abilities or methods.0:47
      You can think of methods asfunctions that belong to an object.0:51
      And you can access methods that an objectowns by using what is known as dot0:56
      notation.1:01
      For instance, there is a greatmethod on strings called upper.1:02
      It creates a new string,1:07
      where all the characters in the originalstring are converted to uppercase.1:09
      It's a good way to makethe computer yell in all caps.1:12
      And I can access the methodson a specific object,1:16
      so lets use our quote object here byplacing a period after the variable name.1:19
      Now, this period here,this is why it's called dot notation.1:24
      And now,I can access the object's methods.1:28
      So that method was upper.1:32
      And it's just like a function.1:35
      So I call it using parenthesis.1:36
      In this particular method,it doesn't take any extra arguments.1:39
      So we can leave the inside of this empty.1:42
      And there,1:45
      you'll see we returned a new string ofEinstein yelling motivation at you.1:47
      And, again,note it didn't change the original string,1:51
      and that's because youcan't change a string.1:54
      It returned a new string.1:58
      So as you can imagine,2:01
      there's also a method namedlower that does a similar thing.2:02
      And so, the string owns the method,lower, and we access it by placing a dot.2:06
      And then, we do the method name,which was lower, and we call it.2:13
      Which the only character herethat's different is that leading a.2:17
      So this is Einsteinwhispering his advice to you.2:20
      Now, if you're heeding his advice,I'd recommend that you pause me and2:23
      attempt to use a method onthe string called title.2:28
      What it does is it turns allwords in the quote to title case,2:31
      meaning each word hasa capitalized first letter.2:35
      Go ahead and try it.2:38
      Pause me.2:40
      Are you ready?2:41
      Here I go.This is what I did.2:43
      I did quote, and then I did a dotto access its methods, title, and2:44
      then I called it.2:48
      So that could be the title ofyour autobiography, am I right?2:52
      You can also turn most objects into theirstring version using the built in str,2:55
      which is short for string.3:01
      Now, this works the same waythat we coerce strings to ints,3:03
      it's just the reverse, right?3:06
      So we could say str(42), and we'll getback the string 42, not the integer 42.3:07
      If you're curious to see whatother methods a string owns,3:13
      you can see them by doing help andan (str).3:16
      So you'll note we have methods definedhere, and they're in alphabetical order.3:22
      Now, these ones that startwith these double underscores,3:27
      these are known as magic methods.3:30
      So let's skip past those for now,we've got plenty of magic already.3:32
      So, the first keep on pressingspace bar it's jumping.3:37
      So there we go.3:39
      I think I saw the first oneoutside of there is capitalized.3:40
      So here,this capitol S represents our string.3:44
      And you can see it'scalling capitalize with no3:50
      arguments because it doesn't take any.3:52
      And it returns a new string.3:54
      That's what this arrow means,it means it will return you a new string.3:57
      And the instructions of what it says is itwill return a capitalized version of S and4:01
      make the first character haveupper-case and the rest lower-case,4:04
      which our string already does.4:07
      Now please,4:09
      don't feel like you should be able toread all this documentation right now.4:10
      We'll get there, but you should bestarting to recognize some words and4:14
      concepts.4:18
      Just like foreign language immersion.4:19
      Isn't that kind of cool?4:21
      Now unfortunately, most documentation isnot written with a beginner in mind, and4:23
      some terms andknowledge are taken for granted.4:27
      But hey,that's what we're here for, right?4:30
      We'll get you reading this typeof documentation in no time.4:32
      Keep immersing yourselves.4:35
      Okay, now press Q to drop outof the help documentation.4:37
      And I'd like to show off a prettyhandy feature of strings that you'll4:40
      use quite a bit.4:44
      It's called string formatting,and it allows you to create4:45
      a reusable template that can bepopulated with different data.4:49
      Think of these kind of like a mail merge.4:53
      For instance, let's build one foran e-mail subject for treehouse students.4:56
      The e-mail subject when populated wouldprobably look something like this.5:01
      We'll say thanks for learning JavaScript5:04
      with us Craig.5:10
      That's just one of the topicsthat we teach is JavaScript.5:12
      So what we could do is we coulddefine a template just like this for5:14
      all topics and for all students.5:18
      And all we would have to do isjust switch out the variable.5:20
      So what you do is you create a variable.5:23
      We'll call it subject_template, andwe'll make it so we can reuse this later.5:25
      And now, this could be named anything,don't worry about subject_template.5:31
      And what happens is you put yourstring that you want your template.5:35
      So we say thanks for learning.5:39
      And then, when you want to replacesomething, you add a placeholder.5:41
      And those look like this.5:44
      It's an open curly bracket andthen a closed curly bracket.5:46
      So Thanks for learning { } with us.5:50
      And then,we're going to also change the name out.5:52
      We're going to put another placeholder.5:55
      So we'll do an { } and we'll do an !.5:57
      Close that.6:02
      So now that we have a template,we can fill it with data.6:05
      So we're going to usethe format method on strings.6:08
      It is a method that they own.6:12
      subject_template.format.6:13
      And the way that this works is you putin the first parameter, we'll replace6:17
      the first place holder and the second onewill replace the second place holder.6:21
      So the first parameter that we wantto push in, we want to say thanks for6:25
      learning Python with us.6:29
      And so, Python is the topic.6:32
      And Valentina is the namethat we'll use here.6:34
      Thanks forlearning Python with us Valentina!6:38
      Awesome, right?6:41
      And you can keep using different strings,right?6:42
      So here, we also teach Java.6:44
      Somebody that I appreciateon our team here.6:47
      We have Thanks forlearning Java with us Shadd.6:52
      Awesome, right?6:54
      Now, numbers willautomatically be coerced.6:57
      For instance,if we had a purchasing screen,7:01
      like something said you just bought,and then we put a placeholder for7:02
      a number of items, and then we puta placeholder for what the item was.7:08
      So we say you just bought,now we'll do .format,7:13
      and we can pass in 3 and fidget cubes.7:17
      And there,you see you just bought 3 fidget cubes.7:21
      See how the 3 was automaticallycoerced to a string?7:26
      But wait, there's more.7:29
      One more powerful feature of stringsis that you can check to see if7:31
      one string is contained in another.7:35
      Now, you can do this with the keyword in.7:37
      It ends up reading very clear.7:40
      So for instance, if you want to see isthere ham in hamster, It returned true.7:43
      This is saying that this expressionis true with a capital T.7:50
      It is true that the wordham is actually in hamster.7:54
      And the reverse is also true.7:58
      Is the word popcorn in hamster.8:00
      It's false.8:04
      And you can see that, right?8:07
      It's returned us backa False with a capital F.8:08
      Now, these true and8:11
      false values are actually a differentdata type that we should explore.8:12
      These are called Booleans.8:16
      It's true that we're about totie up this string exploration.8:18
      So take a quick break and then come backrefreshed ready to dive into Booleans.8:21

      You need to sign up for Treehouse in order to download course files.

      Sign up

        You need to sign up for Treehouse in order to set up Workspace

        Sign up