How to Change Type of a Variable in Python


In this example we will show how to change the type of a variable after it has been set in Python.

Source Code

var1 = 20         # var1 is of type int
print(var1)

var1 = '20'       # var1 is now of type str
print(var1)

var1 = 'python'   # var1 is re-assigned
print(var1)

Output:

20
20
python
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments