Hi, welcome again. In the previous lecture we were talking about numbers.
Specifically we looked at integers and float data types so these are referred
to as data types in Python, so integers and floats. In this lecture we're going
to look at strings. I already mentioned strings in the previous lecture.
So a string is everything that goes inside quotes. "Hi there". That is a string.
And of course strings can be stored in variables and you can print those
variables, so we're using the interactive shell now, but of course you can use a
part of script as we did previously. So numbers were used to do math operations,
but strings, strings are used for displaying information for the user.
So let's say you have a web application, and you want to generate some text
dynamically on the web page, so you'd use would use Python on the backend and
Python would send strings to the HTML code and HTML would render the
code on the web page basically that's the idea or if you want to create a
notepad, an editor with Python such as Atom let's say, all these things you see
here so my program.py selection file, so all this would be written as
strings inside Python and then you display them on the interface using
various functions, for instance if you want to display a string on the terminal
you'd use the print function. Now strings have methods associated with them. What I
mean with methods is let me go ahead and try out a method. Methods, for
methods you need to use the dot notation. So you'd refer to the string which is
string c in our case actually variable c, but the variable is holding
the strings so it's the same as doing here dot, so let's do c dot, replace and
what I want to replace is e with i. So I'm replacing, for string here I'm
replacing letter e with later i and what that will do it will replace all
the letters, all the occurrences of this letter in the string, so you'd ask where
can we see these method? Well that's very easy actually. You can do dir and
then pass the string there, any string. It could be c, it could be an empty string
or whatever and that will print out all the available methods that you can use
with strings, so let's say c dot upper. That will cover the strings to
upper case letters. Now if you want to know what these methods are about you
could ask for help. Just do help and then c, so the
string or just empty string dot
here replace method of the string instance, so string is like the blueprint
that creates strings objects or you can say the data type that represents texts.
Look here. Here is the syntax, so what you do is, you use a string like we did.
So let's say here replace. Then you open brackets. Here is the old item you want
to remove from the string and then the new item we want to replace it with i and
then what you see here in square brackets, that means this parameter is
optional so you can either pass it or not and you know in the previous case we
didn't pass it so what Python did was it replaced all the occurrences of the i
item but if you want to replace only the first occurrence you do one there and
that will replace all the first i over the first e with i. That's how it works
So strings, they support functions, so print is a function. Here. And they support
methods like this one here and they also support operators so you get "Here"
plus "There" and you get "HereThere" or you can make it more readable like that so
an empty space is also a string and yeah that's about strings for now and I'll
talk to you later.