Computer science
-
Introduction to Programs Data Types and Variables
-
Binary Numbers
-
Python Lists
-
For Loops in Python
-
While Loops in Python
-
Fun with Strings
-
Writing a Simple Factorial Program. (Python 2)
-
Stepping Through the Factorial Program
-
Flowchart for the Factorial Program
-
Python 3 Not Backwards Compatible with Python 2
-
Defining a Factorial Function
-
Diagramming What Happens with a Function Call
-
Recursive Factorial Function
-
Comparing Iterative and Recursive Factorial Functions
-
Exercise - Write a Fibonacci Function
-
Iterative Fibonacci Function Example
-
Stepping Through Iterative Fibonacci Function
-
Recursive Fibonacci Example
-
Stepping Through Recursive Fibonacci Function
-
Exercise - Write a Sorting Function
-
Insertion Sort Algorithm
-
Insertion Sort in Python
-
Stepping Through Insertion Sort Function
-
Simpler Insertion Sort Function
Fun with Strings Experimenting and seeing what we can do with strings
⇐ Use this menu to view and help create subtitles for this video in many different languages.
You'll probably want to hide YouTube's captions if using these subtitles.
- What I wanna do in this video is
- get ourselves a little bit more comfortable
- with the idea of strings.
- And also see the power of strings,
- and all the things we can do to them.
- or do with them.
- So what I'm gonna do up here is
- I'm gonna write a little simple program
- that will essentially just define a bunch of strings for me.
- And then I'm gonna use the Python interpreter
- to play with those strings
- and see what we can do to them.
- So let me define a string.
- Let's call the string "a"
- and "a" is—
- let's call it "My first test string".
- "My first test string"—
- right over there.
- As you know, a string is just
- a big sequence of characters right over here
- and you could recognize them
- because they'll either be in single
- or double quotation marks (' or ").
- And just to make clear that
- it doesn't have to be double ("),
- let me define "b" to be
- 'Another test string that I have defined'
- And just to clarify on how you can define strings,
- you can actually put—
- quotations within the quotation marks
- as long as it's clear where the string begins and ends.
- So let's say you have something like this:
- So let's say you say "This is Sal's string"
- So in this example,
- this apostrophe is okay—
- it's a single quotation (') but it's okay
- because the interpreter will know that,
- "Look, the string starts with a double quote (")
- so I have to look for
- another double quote (") for it to end."
- So it's not going to say,
- "Oh, it ends at the single quote ('),"
- it says, "I need a double quote (")."
- So it knows that this whole thing is a string
- and it also knows that this apostrophe is just a character.
- One thing I couldn't have done—I can't do:
- Let's try to define string "d" here.
- So let's say, let me define it this way.
- So I couldn't have done this:
- 'This is Sal's string'
- This doesn't make any sense,
- because here, we're opening with a single quotation,
- then it'll close right
- when we get to the next [single] quotation,
- and so all of this [s string'] is just gonna be some text
- that the interpreter is going to
- try to view as some type of program,
- and it's going to break on that.
- So if you want this to work,
- you can't have it like that.
- You can, you could do something like:
- 'Sal's—no, you can't do that—
- So 'My favorite...
- and there are ways to do that
- and we're not gonna go into escape characters
- and all of that right now,
- but, 'My favorite word is...
- and actually I don't know what my favorite word is,
- I'm kind of putting myself on the spot.
- But let's say 'My favorite word is "asparagus"...
- As...I can't even spell it.
- As...asparagus.
- So 'My favorite word is "aspara[g]us", what is yours?'
- So once again this is legitimate because
- I start my string with a single quotation mark,
- so it says,
- "Look, let's not end the string
- until we get to another single quotation mark."
- So it's not like it'll confuse the interpreter
- when it sees this first double quotation
- 'cause it knows that
- the string started with a single quotation.
- Let me show you some other examples of strings,
- and these will be interesting to deal with.
- And they don't always have to be named [labels?]
- so let me call this my "math_string"
- And let's call this—let's say this is "3+4*2"
- So that's a math string.
- it's really just a string of characters.
- The interpreter won't evaluate it,
- it literally just views it as a string of characters.
- So I think this is a pretty good—
- let me do one other kind of pseudo-math-string
- or let me do another expression,
- let me call this an expression string.
- So that's just the name of the variable,
- and let me say, my expression string is...
- let's say it's "a+...
- and then I'm have a kind of string inside of that string (' ')
- ...+b+' tiger'"
- right over there.
- So this is just—
- so everything inbetween the double quotation marks
- the interpreter is just going to view
- as a bunch of characters.
- Well, there are interesting things
- that we might be able to do with this eventually.
- So that's enough strings for me to define,
- so let me save this file right now and let me run it.
- And what this does—
- now in this environment all of these strings are defined
- and I can verify it by that I can say
- "a", 'My first test string'
- "b", 'Another test string that I have defined,'
- "c", "This is Sal's string"
- "d", 'My favorite word is...
- "aspara...
- "asparaus"...
- I couldn't even spell "asparagus" properly,
- maybe we'll have to fix that.
- ..."asparaus", what is yours?'
- That needs a "g", asparaGus!
- ...What is yours?'
- And then we have—
- it's a good thing this isn't the spelling video—
- we can look at what math_string looks like,
- math_string, it's literally that string of characters.
- And then we have the expression string.
- The expression_string literally just that string of characters.
- Now let's start to play around
- with some of these strings
- and see what we can do with them.
- So, a couple of things,
- we can say, "Hey, how long is a string?"
- And there we could use the built-in Python function len()
- which is short for length.
- So the len() of a [len(a)],
- it says, "Hey, there's twenty character in a!"
- Let's count it, let's verify it.
- 1, 2, space (' ') is a character,
- [counting to twenty]
- Yes, twenty characters!
- What's the the len() of math_string?
- len(), length for short.
- math_string is five characters.
- Let's count 'em.
- 1 2 3 4 5 characters.
- Makes a lot of sense.
- We could do it for any of them,
- and I encourage you to try this out yourself;
- really experiment, become comfortable with this.
- Now, the next thing I want to do is show you
- how to concatenate two strings.
- Sounds like a very fancy word,
- but it really just means connect them together.
- So, for example, I would say,
- let's create a new string, let's call it,
- "a▁with▁b", and I'll define it as a+b
- So I'm doing something very interesting here.
- You're used to using the addition operator (+) with numbers,
- but I'm about to add two strings.
- Let's see what this looks like.
- So, let's look what a▁with▁b looks like.
- a▁with▁b is just a variable.
- Now, what happened?
- I had a, which is "My first test string,"
- and I had b, which is
- 'Another test string that I have defined'
- When I took a+b,
- it took a, "My first test string,"
- and it added b to the end of it!
- It concatenated b to a—
- concatenate is just a fancy word for "put 'em together!"
- 'Another test string that I have defined,'
- so it merged a—
- I guess you could view it,
- it put b at the end of a.
- And you could go the other way,
- you could call it b▁with▁a—
- let's say that is b+a.
- And then let's look at what that looks like,
- b▁with▁a...
- Now it's the other way around.
- a is added to the end of b.
- So you can do very very fascinating strings, you know,
- if we did math▁string+expression▁string
- I'm not gonna set it to any variable,
- I'm just gonna see what I get if I...
- evaluate that math▁string+expression▁string
- Then I essentially got these two strings added together,
- and so this string got put at the end of this string,
- and you see it right there
- and it's this kind of bizarre looking string,
- but it is a string!
- Everything between these characters,
- everything between the double quotations
- is just viewed as a character.
- These are just characters.
- Now, there's many many many many
- other things we can do with strings,
- I'm not going to introduce you to all of them.
- But strings come with a bunch of functions,
- and if you have an Integrated Development Environment,
- it'll often tell you
- what types of functions you could use.
- For example,
- you might wanna split(),
- maybe you want a list of all the words in a string.
- So maybe I'm writing some type of program,
- and I wanna take this string that's in b
- and I want to put each of these words
- as a separate element in a list.
- So what I can do is, I can say b dot (b.)
- and I'll call one of the methods of b
- and we'll do much more on methods
- and object-oriented progamming and all of that,
- but we can view b as a string object,
- and it has its own methods and can operate on itself.
- For now you can just say,
- "Look, if I wanted to split b into its component words,
- I can cal lthe split() method."
- I could call the split() method,
- and I can say, "What do I want to split it on?"
- So when I say "split,"
- I mean separate this string into a bunch of things
- and put them into a list and split the string
- wherever I see a space.
- So let's see what we return when we get that.
- So notice: it took this string,
- "Another test string that I have defined,"
- it separated the string wherever there's a space,
- and took what's on either side of the space
- and put it as an element in a list.
- Could be useful,
- if you're doing some kind of text processing.
- But I want to show you,
- it doesn't have to split just where you have a space,
- you could say "b.split()"
- and you could say split whenever there's a 't'.
- So you can split on the t
- and this will give you some bizarre-looking thing...
- So notice: every time there was a 't',
- it split the string there and it put whatever
- was on each side of those 't's,
- and put them into a separate element in this list over here.
- So a very fascinating thing,
- you can do things like...
- finding where a character is in a string.
- So let's say you wanna call—I don't know—
- let's try the math_string,
- and I wanna find in the math_string,
- (one of its methods),
- I want to find that asterisk
- (another word I have trouble saying).
- So find() the asterisk.
- Tell me which character, if any, has an asterisk in it.
- It tells us the third character does.
- Let's verify it.
- So this is the zeroth character,
- that's the beginning,
- that's the convention in most computer programs.
- Zeroth character,
- first character, second character, third character...
- is an asterisk.
- If you wanted to find the 3,
- and I really encourage you to experiment with this,
- find() the 3!
- It's the zeroth character there.
- Now, there's other stuff,
- you can replace() characters, you can say,
- "Look, let's take c and let's replace...
- and you can either look it up in a book,
- or just do a web search,
- you can normally find all of the libraries for Python strings,
- or if you have an IDE,
- it'll suggest things that you can do to strings
- and how you do it.
- But let's say we want to replace,
- in string c, all of the 'i's with 'o's.
- Let's see what we get.
- So there you go, it was "This is Sal's string,"
- and it replaced it with 'o's,
- and I want to be clear; it didn't change string c!
- It created a copy of c with the 'i's replaced by 'o's.
- So then it became "Thos os Sal's strong."
- (Sounds like a Nordic language of some kind.)
- And, just to be clear, c did not change.
- But if I took, if I said c = c.replace('i','o')
- then, c has changed to "Thos os Sal's strong."
- Now, the last thing I want to do
- is show you a very magical function
- (at least, from my point of view).
- And that's the eval() function.
- And it exists in interpretative languages like Python
- (and it also exists in JavaScript)
- and what's really cool is, is it can evaluate a string.
- It can view a string,
- you can treat it as a string for a while
- and you can construct it,
- so you could, for example, have a computer program
- that writes another computer program inside a string
- and then evaluates that computer program.
- So that should get you philosophically thinking a little bit.
- So, for example, if I just say math_string,
- it's just a string of characters.
- But if I say eval(math_string),
- the interpreter will actually evaluate what's inside of this.
- So it'll actually treat it as an expression.
- And so I get 11,
- so it takes math_string and it says,
- "Okay, now let me treat that like a program,"
- so it's "3+4*2", order of operations,
- does the 4*2 first so that's 8.
- 3 + 8 is 11.
- And you can do even cooler things!
- You can do "eval("—
- let's think about this—"math_string"—
- and let's add to math_string a character,
- the character '1'.
- And let's see what it gets us.
- It gets us 87, what did it do?!
- Well, what it did is,
- math_string is this stuff but if you add a 1 to it,
- it would look like that [3+4*21],
- the 2 would become a 21,
- we're concatenating a 1 to the end of it.
- So it became 4*21 which is 84,
- +3 which is 87.
- And you can do other things,
- we can evaluate this expression_string right over here,
- that's why I set it up like that.
- eval(expression_string)
- Really fun to play with,
- I could do this all day!
- eval(expression_string)
- Remember, expression_string is just a string,
- but when we evaluate it,
- you might want to pause and think what you're going to get.
- So let's evaluate it.
- I got all this craziness! Where did that come from?
- Well, in the string, these were just characters, 'a' and 'b'
- but when I evaluated it in our current context,
- a and b are variables and they represent strings.
- So when you evaluate this thing,
- it's gonna say, "Okay, a is a string,
- it's 'My first text string,'
- concatenate or put this space at the end of it".
- So, "My first test string"
- and then I have a space,
- and then, put b at the end of that,
- so "Another test string that I have defined,"
- then put...
- then put a...
- SPACE TIGER
- and then you have a space tiger.
- So I'm gonna leave you there...
- as you can imagine,
- strings are a super super duper powerful concepts
- for doing a bunch of applications.
- And I haven't exhausted anywhere
- near all the things you can do with them,
- so I encourage you to experiment.
Be specific, and indicate a time in the video:
At 5:31, how is the moon large enough to block the sun? Isn't the sun way larger?
|
Have something that's not a question about this content? |
This discussion area is not meant for answering homework questions.
Discuss the site
For general discussions about Khan Academy, visit our Reddit discussion page.
Flag inappropriate posts
Here are posts to avoid making. If you do encounter them, flag them for attention from our Guardians.
abuse
- disrespectful or offensive
- an advertisement
not helpful
- low quality
- not about the video topic
- soliciting votes or seeking badges
- a homework question
- a duplicate answer
- repeatedly making the same post
wrong category
- a tip or feedback in Questions
- a question in Tips & Feedback
- an answer that should be its own question
about the site
Share a tip
Suggest a fix
Have something that's not a tip or feedback about this content?
This discussion area is not meant for answering homework questions.