Python for everybody | 6 | What are data types in Python programming

Python Tutorial 6
What Are Data Types (1)

In this tutorial, we will cover Part 1 of what are data types in Python programming.

We will cover data types in more detail in the tutorial on String Manipulation.

Without further ado, let’s get started!

What Are Data Types In Python Programming


So what are data types in Python programming?

Computers are nothing more than machines that do 2 specific tasks extremely well — which are to store values and to perform operations on these values.

All computers achieve complex tasks by breaking down these complex tasks into multiple smaller and simpler operations that are performed on simple values.

What are data types in Python programming | 1

String Data Type

The first data type that I am going to talk about is strings.

Fundamentally, a string is a sequence of characters.

What are data types in Python programming | 2

Strings can include alphanumeric characters and special characters:

What are data types in Python programming | 3

We enclose strings using an open quotation mark and a close quotation mark.

What are data types in Python programming | 4

Strings with empty values can be represented by 2 quotation marks with nothing between them.

They are called empty strings.

In the Hello World example, we can also use variables to achieve the same result by first assigning the string literal “Hello World” to a variable and then printing the variable instead of printing the string literal.

What are data types in Python programming | 6

greetings is the variable name — it is a container or a storage box.

“Hello World” is what we call the string literal which is the contents or value of the string and is always enclosed within quotation marks.

The words “Hello World” are first stored in the variable greetings.

Whatever that is stored in the variable greetings is then passed on to the print() function which will print out the contents of the variable greetings.

Double Or Single Quotation Marks – Stay Consistent

Take note that strings are enclosed using either the double quotation marks or the single quotation marks:

But remember to be consistent in our usage and adopt either only the double or only the single quotation marks throughout our program.

So if we use double quotation marks, then stick to it throughout our program.

Triple Quotation Marks

In addition, in Python we can use triple quotation marks formed by either 3 single quotation marks or 3 double quotation marks to enclose a string literal.

Triple quotation marks allow string literals to span multiple lines.

Example using single quotation marks:

print(multi_line_text) will print the whole string with each line on its own separate line.

Example using double quotation marks:

print(multi_line_text) will print the whole string with each line on its separate line.

The Escape Character

If we have quotation marks within our string literals we can precede them with what is called the “escape” character which is the backslash character \ on our keyboard.

If we enclose our string literal using double quotation marks, we must escape any double quotation marks within our string literal by preceding it with a backslash character.

But if we enclose our string literal using single quotation marks, we must escape any single quotation marks within our string literal by preceding it with a backslash character.

Since the backslash character inside a string literal is interpreted as an escape character, what do we do if we need to include the actual backslash character in our string literal?

The answer to this – is to backslash our backslash character.

By putting a backslash character before a backslash character, we are effectively telling Python that the second backslash character is to be interpreted as an actual backslash character.

So if we do not backslash a backslash character:

Python will generate an error message.

Why?

Because the backslash character within a string literal is interpreted as an escape character and immediately following an escape character it expects either a quotation mark or another backslash character — which it could not find.

The Raw String

Alternatively, we can do what is called “raw string” where we precede our string literal with an “r” character.

The “r” character tells Python not to treat the backslash character inside the string literal as an escape character — but rather to treat the backslash character as a backslash character.

Our code will look neat as well as elegant when we use the “raw string” method whenever we need to tell Python not to treat the backslash character inside a string literal as an escape character.

So always strive for neat elegant code that makes it easy to read and understand.

Watch my online Python course for a different learning experience.

>> Click here to watch my online Python course at Udemy