Hello everybody and welcome back to this tutorial. And in the previous tutorial we
took a look at the different math operators that can be used in the Python 3
interpreter, whether you are using simply just numbers as within the previous
video, or whether you're actually storing different numbers in variables and then
using them as variables. But what we are going to take a look at now are some
strings in Python 3. So, first of all, let us
think what an actual string is. So we know that a number is simply a number,
but Python 3, or our program, has to know when we actually want to type
a word, or a sentence, or any paragraph, or something like that. What a string is, is,
basically, strings are words. Now you define strings in
Python with the actual single or double quotes. So, for example, if you just use
'hello', you can see this will print 'hello' as a string. If you
just type hello without single quotes it will give you an actual error saying
hello is not defined. Because if you don't have the quotes it will try to
search hello as an actual variable. And we have not defined any hello variable
so therefore we cannot really find hello, and therefore we get the error. You can
also use the actual double quotes if you want to. So you can see right here "hello"
it will print the same output as in these single quotes, which is just simply
"hello". So, this is what a string is. Basically, a word, or paragraph, or an
actual sentence defined between the single or double quotes.
Now what is interesting in Python 3 is that these strings, or the
math operators can actually be used on strings. Not all of them, but some of them.
Such as the addition, the multiplication, you can use that on strings, and we will
show just in a second how you can do that. So, just open up your IDLE, whether it is
in the Ubuntu, like I'm doing right here, or whether it is in your Windows 10
environment. So just open it anywhere you like. Let us use the Windows 10 of our
right now, doesn't even matter. And first of all before we actually take a look at
the math operators using with the strings, let us see how we can actually manipulate
strings differently. So, if you just type here 'hello', and then
add a + right here, and then open single quotes once again and just type
here 'world', you can see that we actually get printed out 'helloworld'. Now you
might be asking right now, first of all, why did this actually get printed out as
one word, and, second of all, why this doesn't have any space between
it. Well, the plus sign can actually concat strings for us, same as with
numbers. If you, for example, type 2+2 it will give you 4. But, if
you type string plus string, it will add the second string to the ending of the
previous string. And the reason why we don't have any actual space between
these two words is because we didn't define any space in our strings right
here. So if you want to have space you can just type 'hello ', and then add space
and then specify the single quote, and then + 'world'. And you can see right
now we get printed out 'hello world'. Now of course you don't have to do this
like this. You can simply just type open quotes 'hello world' and then close
quotes and it will print the same thing without using the math operator +, and
concatenating two different strings. Now one thing you can't do is you can't,
for example, concat strings with numbers. If you try to do something like this, so
'hello' + 4, it will give you
an error, can only concatenate string to string.
What did I even say? Doesn't even matter.
So you can only concatenate strings, not integer to string. So what this basically
says is you can concat two different types of actual variables. So, for example,
you can't concat an integer or a number to a string, which actually makes sense.
You can only add numbers with numbers, and you can only add strings with strings.
However, you can actually multiply strings with numbers. Since we can't add
it we can try to multiply it. So if you just 'hello' and
then * 4. You can see what we get as an output is an actual hellohellohellohello
printed out four times. Now if you want to print it a hundred times you can
do that, so 'hello' * 100, you will get 'hello' printed out 100 times.
So, let us see how we can actually do it with variables, even though we haven't
really covered variables, yet. Let us see, I will just show you an example of
how this would be useful with variables. So let's say string1 =, so just
copy the same names. I will explain what kind of names variables need to have
later on. For now on just call it string1 = 'hello'.
string2 = 'world'.
And let's see how we can actually instead of just typing the actual
strings themselves, which really wouldn't make any sense. For example, let's say
that you need to type a whole entire paragraph which will be stored in one
string. It is rather easier to just type the variable name than to type the
entire paragraph when you want to use it. So in order to concat the strings we
can just add string3 which will be equal to string1 + string2.
You can see everything worked without an error. And if I just want to print the
string3, which will be the value stored in string3, you can see we get printed
out helloworld, which is normal and that is what we expected. But right now if we
wanted to change the string3 we just simply type in string1 *
4 + string2. And if you just type here string3 you can see
'hellohellohellohelloworld'. So what it does first since our string1 has the
word 'hello' in it, it was multiplied 4 times as
we can see right here, and then at the end it will add the string2, which in
our case is just 'world'. So you can perform something like this as well. Or,
basically, this is something that you will perform. Most likely, you will not be
doing anything like this, since this only works
in the interpreter itself. If I wanted to write this in any text editor or in
any program that you are writing, this will give you out an error saying
basically nothing right here is really defined, therefore the program doesn't
really know what to do with this. But if you write it something like this, this is
something that the Python program will recognize and it will execute this,
printing the 'hello world' string. This as well
right here. But, of course, if you're
wondering, you can't really divide strings. So, string3 =
string1/string2 will give you out an error saying unsupported
operand type. So basically you can't really divide strings because we don't
really know what that would be. You can only multiply and add strings. So that
would be about it for this simple, basic introductory to the strings themselves.
Of course, we'll manipulate them on a whole other level later on, but for now
on this will be enough. If you're a beginner this should be enough to
actually understand strings at the most basic level, and we are going to continue
coding and learning in the next lecture. So hope you enjoyed this tutorial and I
hope I see you in the next one. Bye!