Learn Python Complete Series.
Lecture 7: Outline
- Python Casting
- Specifying variable type
- IntegerCasting, stringCasting, and floatCasting
- String literals
- Assigning String to a Variable
- Multiline String
Specify a Variable Type
Sometimes we need to specify a variable on a specific datatype. This can only be possible through casting.
A particular task of assigning or specifying a particular datatype to a variable is called ”_casting_. ”
Python as OOP (ObjectOrientedLanguage) uses classes to define dataTypes, including its primitiveTypes.
For achieving casting, use constructor_Functions:
constructs an integer int number from an integerLiteral, a floatliteral by roundingDown to the previous wholeNumber in Python, or a stringLiteral (the String denotes a wholeNumber);
constructs a floatNumber from integerliteral, a floatLiteral or a stringLiteral (the String denotes a float/integer);
constructs string str from a various datatypes, including stringsLiteral, integerLiterals, and floatLiterals in Python;
g = int(7) #the value of g is here 7 because of int value
h = int(7.8) #the value of h is here 7 because of rounding up its int
i = int(“7”) #the value of i is here 7 because of intType
Let’s check the example below regarding int datatype casting.
Floats Casting in Python:
g = float(7) #the value of g is here 7.0 because of float dataValue
h = float(7.7) #the value of g is here 7.7 because of float value
i = float(“7”) #the value of g is here 7.0 because of float value
j = float(“7.2”) #the value of g is here 7.0 because of float value casting
Let’s check the example below regarding float datatype casting.
g = str(“g1”) #the value of g is here ‘g1’because of string value casting
h = str(7) #the value of h is here ‘7’because of string typeCasting
i = str(7.0) #the value of i is here ‘7.0’because of string typecasting
Let’s check the example below regarding String(s) datatype casting.
NOTE: casting automatically converts any value to its own type, which is particularly defined before printing…
You can put string value by just assigning a string value to a variable in Python.
In Python, you can write String literal(s) using’ ‘single quotes mark or
” ” double quotes marks.
‘hurry’ is same like “hurry”.
For printing the String literal you can use PRINT() method:
Let’s check the example below;
Assigning String to a Variable
String assignment is done with the variableName followed by an “=” sign and the String:
g = “Hurry”
Let’s check the example below;
Use triple quotes to assign a multilineString to any variable.
You can use 3time double quotes, ” ” ” double quotes:
g = “”” hey this a demo content with triple quotes, just for your understanding and making your mind clear about multiline strings. Further, For multiline string use triple quotes and put text in it”””
Let’s check the example below, triple quotes;
Or it can also be possible in three single quotes;
h = ‘”hey this a demo content with triple single quotes, just for your understanding and making your mind clear about multiline strings. Moreover, in Python, For multiline String use three single quotes and put text in it'”
Let’s check the example below, three single quotes;
Both are working same:
Note: As a result, the “LineBreak” is inserted at the same position as in the programCode.
Also, Visit official site of Python here.