Hey everybody and welcome to the next video.
So in last video we talked about variables.
So we talked about integers boolean strings etc. But what we're going to go over today is something
called typecasting.
Now typecasting is when you want to convert one type of value to another type.
So if you want to convert a string to an integer or an integer to a string as an example.
Now this is important because in some cases you might have something called you know let's say temp
string is set to five right for whatever reason it's user input and it comes in as an integer or it
comes in as a string.
However you want to be able to use that in some mathematical formula.
So let's say we want to do result is equal to you know five plus tapestry.
Right now we want to print out result.
Now if we do that
looking for you if we do that you're going to see this error unsupported operating types for plus of
end and a string.
So instead of the string it's failing because we're trying to perform some mathematical operation or
any operation on a string and a number.
So what you can do if you have a string and you want an end you're going to do something called INT
and then in the quotes.
Right.
So what this is going to do is this is going to cast this string as an integer.
So what happens behind the scenes since you're familiar with the ascii table.
It's going to look up what this is what the digit of this is on the ascii table we have.
So we go in here.
Now we can see we get our answer of 10 without any errors.
Now this is great.
However when we print something for example we printed result or reprinted we'll see temp string right.
It's still going to print correctly.
The reason this happens is because it actually converts this to a string on its own.
However if we use the type function here and we print out the type of temp string after we've casted
it you can see that it's still considered a string.
The casting only happened at that point in time.
However if we did temp string is equal to int temp string.
So now we're casting it in reassigning it to temp.
So now we should come back with a string earn it say now we've cast it and completed the transformation
because we've actually set it back to itself.
So casting only works for the immediate time being just make sure to understand how casting works.
So the same thing goes with string.
So if we wanted to convert if we had a temp and it's five.
Right.
So if we let's go and change this up.
So now we have now we're gonna print out the type of and we should be able to get it perfect.
Now we can do this.
We can even cast it inside of the type.
So s.t. are similar to what we did for int in our quote.
This one did took away.
So we can see now it's type string because we casted it before we passed it over.
You can do the same thing with string.
Now there's a few other ways you could typecast to to different types of different variables to other
different types.
However Python can do some conversion on its own.
So when you tried to do a an integer and then adding to a float as an example the integer will be converted
to a float with a point zero at the end.
If it's a solid number so in this case we had five and then 5.1 we would add it would take the five
and make it five point zero and then it would just add it together to be ten point one.
So Python can do some typecasting on its own but when it comes from strings to its and vice versa you're
going to want to do that yourself to make sure it's explicit which is what this type of casting is.
So feel free to go out look a little bit more on typecasting it is important to understand some of the
options that you have available because there's gonna be some cases where data's not in the correct
type that you're expecting.
And this can also be used to to determine what type you want.
So for example that type function here that type function allows you to check if a type is a string
or if it's an integer.
So when some input comes in you know what to expect and whether you have to cast it or not.