Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python casting

# Casting = Specify a type on to a variable.
# Strings
x = str("s1") # x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'
# Ints
x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
# Floats
x = float(1)     # x will be 1.0
Comment

python casting

print(str(1))  # convert number to string
print(int("1"))  # convert string to int
print(float(1))  # convert int to float
print(list('hello'))  # convert string to list
print(tuple('hello'))  # convert string to tuple
print(list((1, 2, 3)))  # convert tuple to list
print(tuple([1, 2, 3]))  # convert list to tuple
print(bool(1))  # convert a number to boolean
print(bool(0))  # convert a number to boolean
print(bool(""))  # convert a string to boolean
print(bool("data"))  # convert string to boolean
print(bin(10))  # convert an integer to a binary string
print(hex(10))  # convert an integer to a hex string
print(oct(10))  # convert an integer to an octal string
Comment

casting in python

x = "3"
int(x)

y = "2.6"
float(y)

z = "1j"
complex(z)

print(typeof(x))
# Int
# float
# complex
Comment

casting in python

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

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

x = str("s1") # x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'
Comment

python type casting

var_as_num = 10
var_as_str = str(var_as_num)
Comment

Python Type Casting

x = int(1.1)
y = str(1.1)
z = float(1.1)
print(x)
print(y)
print(z)
Comment

Python Variable Type Casting

x = str("Jack");
y = int(111)
print(x)
print(y)
Comment

PREVIOUS NEXT
Code Example
Python :: Python numpy.flatiter function Example 
Python :: higlight words in python 
Python :: python if statement 
Python :: create a django project 
Python :: escape sequence in python 
Python :: list in list python 
Python :: square a number in python 
Python :: res.send is not a function 
Python :: python generate set of random numbers 
Python :: serialize keras model 
Python :: how to declare np datetime 
Python :: Django migrations when table already exist in database 
Python :: split and only grab first part of string 
Python :: Python connect to a server via RDP 
Python :: polish notation python 
Python :: dict keys to list in python 
Python :: sub matrix python 
Python :: python community 
Python :: hash with python 
Python :: add python to path 
Python :: python datetime move forward one day 
Python :: python test type 
Python :: python turn off garbage collection 
Python :: get file parent directory python 
Python :: python replace with something else 
Python :: how to make an array in python 
Python :: valid parentheses 
Python :: list reverse method in python 
Python :: code for merge sort 
Python :: code coverage pytest as html 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =