Fun with Strings

Fun with Strings


What I want to 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 going
to do up here is I’m going to write a
little simple program that will essentially just define
a bunch of strings for me. And then I’m going to 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.” Right over there. As we know, A string is just
a big sequence of characters. Right over here. And you can recognize them
because they’ll either be in single or double
quotation marks. 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 OK. It’s a single quotation. But it’s OK 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 to end
with 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 can’t do– let’s
try to define string d here. 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 when we
get to the next quotation. So all of this is just
going to be some text that the interpreter
is going to try to view as some type
of program and it is going to break on that. So if you want this to work,
you can’t have it like that. You could do something like
Sal’s– no, you can do that. So ‘My favorite– And
there are ways to do that– and we’re not going to go
into escape characters and all 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.” I can’t even spell it. So ‘My favorite word is
“asparagus”, 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 because 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 “label.” So let me call this
my math_string. Let’s say this is 3+4*2. So that’s a math_string. It’s literally 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 “a+’– and then I’m going to have a string
inside of that string– ‘+b+’ tiger.'” Right over there. So everything in between
the double quotation marks, the interpreter
is just going to view as a bunch
of characters. But 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 that. I can say, A, ‘My
first test string.’ B, ‘Another test string
that I have defined.’ C, “This is Sal’s test string.” D, ‘my favorite word is
“asparaus”– “Asparaus.” I couldn’t even spell
asparagus property. We’ll have to fix that. “asparaus”, what is yours?’ I
missed the “g”– “asparagus”, what is yours?’ And then we have–
it’s a good thing this isn’t a spelling
video– we could look at what
math_string looks like. math_string is literally
that string of characters. And then we have the
expression_string. Literally, just that
string of characters. Now let’s start to play around
with some of the strings and see what we
can do with them. So a couple of things– we could
see 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 says, hey,
there’s 20 characters in a. Let’s count it. Let’s verify it– 1, 2– space
is a character– 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20. Yes, 20 characters. What’s the len of math_string? math_string, len
length for short. math_string is five characters. Let’s count them– 1,
2, 3, 4, 5 characters. Makes a lot of sense. We could do it for any of them. 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. Concatenated is just a fancy
word for put them together. ‘Another test_string that I
have defined.’ So it merged a. I guess you could view it
as, input 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 going to set
it to any variable. I’ll see what I get
if I evaluate that. Then I essentially got these
two strings added together. And so this string got put
at the end of this string. 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 quotation, is just viewed as a character. These are just characters. Now there’s 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, maybe
you want a list of all of the words in a string. So maybe I’m writing
some type of program. And I want to take the
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. and I’ll call one of
the methods of b– and we’ll do much
more on methods and object oriented
programming and all of that– But we can view b as
the string object. And it has its own methods
that can operate on itself. For now, you could
just say, look, if I want to split b
into its component words, 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 the 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’ve 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 type of text processing. But I want to show
you that it doesn’t have to be split just
where you have a space. You could say
b.split, and you could 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 what was ever
on each side of those t’s into a separate element
in this list over here. So very fascinating things. I mean, you can do
things like finding where a character
is in a string. So, let’s say you want to call–
Let’s Try the math_string. And I want to find
in the math_string– so it’s one of its methods– I
want to find that asterisks– another word I have
trouble saying. So find the asterisks. 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
zero-th character. That’s the beginning. That’s the convention in
most computer programs. Zero-th character, first
character, second character, third character is an asterisks. If you wanted to
find the three– and I really encourage you
to experiment with this– find the 3. It’s the zero-th
character there. Now, there’s other stuff. You could replace characters. You can say, look, let’s take
c and let’s replace all of– And you can either look it up in
a book or just do a web search. You can normally find all the
libraries for Python strings. Or, if you have an IDE,
Integrated Development Environment, it’ll suggest
things 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 favorite.’
‘This is Sal’s string.’ When you replace 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.” It sounds like a Nordic
language of some kind. And just to be clear,
c did not change. But if I said c=c.replace, and
replace the i’s with o’s, 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 interpreted
languages like Python. And it also exists
in JavaScript. And what’s really cool 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, 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 says, OK, now let me treat that
as like a program. So it’s 3+4*2. Order of operations–
does the 4*2 first. So that’s 8. 3 plus 8 is 11. And you could do
even cooler things. You could do eval– let’s think
about this– math_string– and let’s add to
math_string 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. The 2 would become a 21. We’re concatenating
a 1 to the end of it. So it became 4 times 21, which
is 84 plus 3, which is 87. And you could 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. Remember, expression_string
is just a string. But when we evaluate it–
and 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
in our current context, a and b are variables and
they represent strings. So when you evaluate this
thing, it’s going to say, OK, a is a string. That’s ‘My first test
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 a space tiger. And then you have a space tiger. So I’m going to leave you there. As you can imagine, strings
are super, super duper powerful concepts for doing
a bunch of applications. And I haven’t exhausted
anywhere near all the functions or things you can do with them. So I encourage
you to experiment.

49 thoughts to “Fun with Strings”

  1. I don't really use eval when coding, but is it really a good idea to use that function? It really looks like an easy way to open yourself up to code injection vulnerabilities, especially if any user input goes directly into eval. Although I guess you could sanitize it somehow to make it somewhat safer.

  2. Thank you for uploading this Python stuff. I last programmed back in the 1980s (machine code (really!) and BASIC !?! ) , so have become VERY rusty !
    Your tutorials are excellent. You are a skilled teacher. – With your help, there is , perhaps , hope for my failing brain yet !
    A genuine vote of thanks from me.

  3. I think you would have done a whole lot better explaining it more abstract than spending so much time on the specifics of Python syntax in regard to opening and closing quotation marks and that specific interpreter.

  4. Traceback (most recent call last):
      File "<string>", line 1, in <fragment>
    can't assign to operator: <string>, line 1
    The above is an error I got while I was trying to add the strings a and b in my program. Could anyone help me?

  5. since this video is about what we can do to/with the strings,allow me to ask if it's possible for a pyscripter to interpret 'my first test string' to "a" instead of "a" to 'my first test string'?…i don't know whether that came out right. in case it didn't,what i'm trying to ask is,is it possible to reverse the results without reversing the program?

  6. getting an "can't convert bytes object to str implicitly" error in Python 3.5.1 when trying to do eval(expression_string)

  7. Khan = "this vid is awesome!"

    def khan_changer():
    global Khan
    Khan = "this vid is really awesome!"

    khan_changer()
    print Khan

Leave a Reply

Your email address will not be published. Required fields are marked *