Python Complete Series – 07

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

Python Casting

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:

  • int();

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);

  • float();

constructs a floatNumber from integerliteral, a floatLiteral or a stringLiteral (the String denotes a float/integer);

  • str();

constructs string str from a various datatypes, including stringsLiteral, integerLiterals, and floatLiterals in Python;

Click here for Python 06 Lesson

Integers Casting:

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.

Python Complete Series - 07 1 Top10.Digital

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.

Python Complete Series - 07 2 Top10.Digital

Strings 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.

python complete

NOTE: casting automatically converts any value to its own type, which is particularly defined before printing…

String Literal(s);

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.

Like;

‘hurry’ is same like “hurry”.

For printing the String literal you can use PRINT() method:

 

Let’s check the example below;

python complete

Assigning String to a Variable

String assignment is done with the variableName followed by an “=” sign and the String:

 

g = “Hurry”

print(g)

 

Let’s check the example below;

Python Complete Series - 07 3 Top10.Digital

Multiline String(s);

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”””

print(g)

Let’s check the example below, triple quotes;

Python Complete

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'”

print(h)

Let’s check the example below, three single quotes;

Python Complete Series - 07 4 Top10.Digital

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.

eskort - web tasarım - werbung -

https://www.vozolkapida.com/