James
Active Coder
Hello, I am new to codeforum.org and the main programming language I use is python, so I thought the best way to introduce myself is to make a little tutorial.
In this tutorial I will be showing you how to change the types of values and find out the types of values, example, changing integers to string, booleans to string, strings to integers, etc.
Okay, so let's start with INT - STR
So first, we created the variable "myInt" this is the integer that we will be changing into a string
It's as simple as that.
Now, let us try some other types.
Now to sum all that up,
str() - Turn into string
int() - Turn into integer
bool() -- Turn into bool
Okay, so after we've done that you may now want to know how to find the type of a value.
To do this we simply use the built-in function type()
If you wanted to check if the type is a value you can do something like,
Hoped this helped people out!
In this tutorial I will be showing you how to change the types of values and find out the types of values, example, changing integers to string, booleans to string, strings to integers, etc.
Okay, so let's start with INT - STR
myInt = 5
So first, we created the variable "myInt" this is the integer that we will be changing into a string
Python:
# Here we are going to be using the str() built-in function to change the int to a string
myInt = 5
myStr = str(myInt)
print(myStr)
Now, let us try some other types.
Python:
#Turning a boolean into a string
myBool = True
myString = str(myBool)
Python:
#Turning a string into an int
myString = "555"
myInt = int(myString)
Now to sum all that up,
str() - Turn into string
int() - Turn into integer
bool() -- Turn into bool
Okay, so after we've done that you may now want to know how to find the type of a value.
To do this we simply use the built-in function type()
print(type(55)) # Would output <class 'int'>
If you wanted to check if the type is a value you can do something like,
Python:
if type(55) == int:
print(True)
else:
print(False)
Hoped this helped people out!