0% found this document useful (0 votes)
7 views5 pages

Python Casting

The document explains Python casting, which allows specifying a variable type using constructor functions such as int(), float(), and str(). It provides examples of how to convert integers, floats, and strings using these functions. The document also includes an exercise to test understanding of casting.

Uploaded by

tester
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Python Casting

The document explains Python casting, which allows specifying a variable type using constructor functions such as int(), float(), and str(). It provides examples of how to convert integers, floats, and strings using these functions. The document also includes an exercise to test understanding of casting.

Uploaded by

tester
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4/12/26, 5:52 PM Python Casting

 Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Python Casting
❮ Previous Next ❯

Specify a Variable Type


There may be times when you want to specify a type on to a variable. This can be done with
casting. Python is an object-orientated language, and as such it uses classes to define data
types, including its primitive types.

Casting in python is therefore done using constructor functions:

int() - constructs an integer number from an integer literal, a float literal (by removing
all decimals), or a string literal (providing the string represents a whole number)
float() - constructs a float number from an integer literal, a float literal or a string
literal (providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals

Example Get your own Python Server

Integers:

x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

Try it Yourself »

[Link] 1/5
4/12/26, 5:52 PM Python Casting

 Tutorials 
Example References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Floats:

x = float(1) # x will be 1.0


y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2

Try it Yourself »

Example
Strings:

x = str("s1") # x will be 's1'


y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'

Try it Yourself »

REMOVE ADS

?
Exercise
What will be the result of the following code:
print(int(35.88))

35

[Link] 2/5
4/12/26, 5:52 PM Python Casting

35.88
 Tutorials  References  Exercises  Get Certified Sign In

HTML
 36
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Submit Answer »

❮ Previous Sign in to track progress Next ❯

COLOR PICKER

 
REMOVE ADS

[Link] 3/5
4/12/26, 5:52 PM Python Casting

 Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

-->
 PLUS SPACES

GET CERTIFIED FOR TEACHERS

BOOTCAMPS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
AngularJS Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate

[Link] 4/5
4/12/26, 5:52 PM Python Casting
SQL Examples SQL Certificate

 Tutorials  Python Examples


References 
[Link] Examples
Exercises  Python Certificate
Get Certified
PHP Certificate
Sign In
Bootstrap Examples jQuery Certificate
HTML
 CSS PHP Examples SQL
JAVASCRIPT PYTHON JAVA PHP JavaHOW
Certificate
TO [Link] C
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and
privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

[Link] 5/5

You might also like