# Type Casting In Python

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2FWQSIyufrsDU3azZqTOzn%2Fimage.png?alt=media\&token=0c54047b-6a57-4824-8906-75d4bf563b7b)

## What is Type-Casting?

Type Casting is the method to convert the variable data type into a certain data type in order to the operation required to be performed by users.

In more simple terms, type casting means converting operand's data type to another data type.

## Functions used for Type-Casting

There are 4 important Functions used for Data Type Conversion:

* int() - int() converts any data type to integer data type.
* float() - float() converts any data to float data type.
* str() - str() converts any data type to string data type
* bool() - bool() converts any data type to boolean data type

## int(): Converting any Data Type to integer data type

**Converting float value in integer:**

In \[13]:

```
float_var = 2.5
int_var = int(float_var) 
print(int_var)
print(type(int_var))
```

```
2
<class 'int'>
```

**Converting boolean value in integer:**

In \[2]:

```
bool_var = True
int_var = int(bool_var)
print(int_var)
```

```
1
```

In \[3]:

```
bool_var = False
int_var = int(bool_var)
print(int_var)
```

```
0
```

**Note:**&#x43;onverting boolean value into integer will give 1 if boolean is True and 0 if false. In more simpler terms True means 1 and False means 0.

**Converting string value in integer:**

**Note:**&#x53;tring can only be converted in integer , if it is possible mathematically or if the string is a number. This means that string "ConsoleFlare" can not be converted to integer data type as it is not possible, but string "123" can be converted to 123.See Below:

**Characters as a String: Will not convert**

In \[4]:

```
string_var = "ConsoleFlare"
int_var = int(string_var)
print(int_var)
```

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-88b966b818d7> in <module>
      1 string_var = "ConsoleFlare"
----> 2 int_var = int(string_var)
      3 print(int_var)

ValueError: invalid literal for int() with base 10: 'ConsoleFlare'
```

**Integer as a String: Will convert**

In \[5]:

```
string_var = "123"
int_var = int(string_var)
print(int_var)
```

```
123
```

## float(): Converting any Data Type to float data type

**Converting integer data type in float:**

In \[6]:

```
int_var = 2
float_var = float(int_var) 
print(float_var)
```

```
2.0
```

**Converting boolean data type in float:**

In \[8]:

```
int_var = False
float_var = float(int_var) 
print(float_var)
```

```
0.0
```

**Converting String data type in float:**

**Note:**&#x53;tring can only be converted in float , if it is possible mathematically or if the string is a number. This means that string "ConsoleFlare" can not be converted to integer data type as it is not possible, but string "123.5" can be converted to 123.5.See Below:In \[14]:

```
string_var = "123.5"
float_var = float(string_var)
print(float_var)
print(type(float_var))
```

```
123.5
<class 'float'>
```

**Note:**&#x43;an we convert a String Float (float number in string) in integer data type directly?In \[15]:

```
string_var = "123.5"
int_var = int(string_var)
print(int_var)
```

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-9ccd5336658c> in <module>
      1 string_var = "123.5"
----> 2 int_var = int(string_var)
      3 print(int_var)

ValueError: invalid literal for int() with base 10: '123.5'
```

This will give an error, We cannot convert String float Directly into an integer data type. But there is a WorkAround that we can follow.

* Convert string float to float data type.
* Convert the float value to integer.

In \[16]:

```
string_var = "123.5"
float_var = float(string_var)
int_var = int(float_var)
print(int_var)
```

```
123
```

## str(): Converting any Data Type to string data type

**Note:**&#x73;tr() can convert data type of any variables or values to string data type with no obstacles and no workaround needed.In \[17]:

```
int_var = 4
string_var = str(int_var)
print(string_var)
print(type(string_var))
```

```
4
<class 'str'>
```

## bool(): Converting any Data Type to boolean data type

To Convert Data type of a particular operand in boolean , we must know what that particular value represents , does it represent 0(False) or 1(True).

While converting any value in boolean , outcome will be either non-zero value which is True or zero-value which is false.

Zero-Values: Zero-values are the values that is equal to nothing or 0. Because 0 in its total sense is nothing.

Zero-values: 0 , "" , '','''''' and more where the value is nothing.

Non Zero-Values: Non Zero-values are the values that is not equal nothing. Any value that is not zero or nothing is Non-Zero Value.

Non - Zero-values: "0",123,123.5," "(there is a space between quotes), "12321","ConsoleFlare"

## Converting Zero Values to boolean gives False:

In \[18]:

```
value = 0
bool_value = bool(value)
print(bool_value)
```

```
False
```

In \[19]:

```
value = ""
bool_value = bool(value)
print(bool_value)
```

```
False
```

In \[20]:

```
value = ''''''
bool_value = bool(value)
print(bool_value)
```

```
False
```

## Converting Non Zero Values to boolean gives True:

In \[21]:

```
value = " "
bool_value = bool(value)
print(bool_value)
```

```
True
```

In \[22]:

```
value = "ConsoleFlare"
bool_value = bool(value)
print(bool_value)
```

```
True
```

In \[23]:

```
value = 123
bool_value = bool(value)
print(bool_value)
```

```
True
```

In \[ ]:

```
value = "0"
bool_value = bool(value)
print(bool_value)
```

**Note:**&#x59;ou must have seen in examples that converting 0 (integer type) gives False, but converting "0" gives True. It's because string type 0 is not equal to 0.

## Why do we use Type Casting ?

Sometimes we need to do a particular operation on values but their data type does not allow us to perform such operations, in that case we need to convert its data type to perform desired operation.

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2Fu3vS1Fip3iKszY20EvHx%2Fimage.png?alt=media\&token=0889dc37-0e7d-4bc6-bd7c-54e85576541a)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://training.gitbook.io/python-documents/type-casting-in-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
