> For the complete documentation index, see [llms.txt](https://pythonforstarters.solomonmarvel.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pythonforstarters.solomonmarvel.com/introduction-to-python/type-conversion.md).

# Type Conversion

#### [Type Conversion](broken://pages/3nluqu32B2kxev0xin5p) <a href="#type-conversion" id="type-conversion"></a>

Type conversion is the process of converting a data type into another data type. Implicit type conversion is performed by a Python interpreter only. Explicit type conversion is performed by the user by explicitly using type conversion functions in the program code. Explicit type conversion is also known as typecasting

```
# file name type_conversion1.py

x: int = 20
y: str = str(x)
```

Run Code:

```
python type_conversion1.py
```

The code returns a `string` type showing that the variable `x` was converted to a `string` type.

Let's try another example:

```
# file_name - type_conversion2.py

name = "John Doe"
conv_name = int(name)

print(type(name))
```

Run Code:

```
python type_conversion2.py
```

The code returns an error `ValueError: invalid literal for int() with base 10: 'John Doe'`.

We got the `ValueError` because the value `John Doe` is not a valid number therefore converting a `string` type to a `number` type is only possible if the `string` can become a valid `number`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://pythonforstarters.solomonmarvel.com/introduction-to-python/type-conversion.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.
