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 kivy bind 
Python :: Lucky four codechef solution 
Python :: python import from string name 
Python :: python argsort 
Python :: python library 
Python :: how to draw dendrogram in python 
Python :: boto3 python s3 
Python :: read one column pandas 
Python :: ner spacy 
Python :: remove last digit from number python 
Python :: reshape SAS matrix 
Python :: how to make a 2d array in python 
Python :: extract numbers from list of strings python using regex 
Python :: python variables 
Python :: python referenced before assignment in function 
Python :: generate table python 
Python :: convert pandas data frame to latex file 
Python :: Example of ceil method in python 
Python :: python ON DUPLICATE KEY UPDATE 
Python :: get maximum value index after groupby 
Python :: return the biggest even fro a list python 
Python :: django venv activate 
Python :: destory image in pygame 
Python :: python inline if 
Python :: dictionary input from user in python3 
Python :: python align output 
Python :: assignment operators in python 
Python :: fast api template syntax 
Python :: linux python 
Python :: python delete key if exists 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =