1/5
## Types We can convert types from one to another using something called casting. For example, let's say we had this: ```python year = "2025" ``` This is technically a string because it has quotes around it. If we got rid of the quotes, it would be a number, or an int. But, since it has quotes, it's a string. What if we tried to add 25 to this year number here? We can try this: ```python year + 25 ``` This will result in an error: ```bash TypeError: can only concatenate str (not "int") to str ``` Since 25 is an int, and year is a string, Python can't perform the addition. To fix this, we can typecast the year variable to an int: ```python year_as_int = int(year) ``` Now, we can add 25 to the year variable: ```python year_as_int + 25 ``` This will result in 2050. We can also convert the result back into a string using the `str()` function: ```python year_string = str(year_as_int + 25) ``` Now, we can print the string: ```python print(year_string + " is 25 years after 2025") ``` This will print the string: ```bash 2050 is 25 years after 2025 ``` This is how we can convert types in Python using casting.
A practical guide to data types and casting in Python. The lesson covers how to identify string and integer data types, explains why you can’t add an integer to a string, and demonstrates how to type cast data to overcome type errors.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on January 14, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on January 14, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar