Python Data Types – Strings and Numbers (Integers and Floats)

In this article, we're going to take a quick look at data types in the Python programming language.

Generally, a data type is simply a specific category or variety of data. Just like numerical data is quite different from alphabetical data, the data types in Python have their uniqueness. Let's have a quick look at Strings, Numbers, and Floats data types in Python

STRINGS:

A string is a set of data values that is confined by quotation marks. It can be numerical, alphabetical, or alphanumerical. The most important thing however is to ensure that the quotation marks surround it. We can use double quotes such as:

Value1 = "market"
Value2 = "85"
Value3 = "method 2"

Single quotes can also be used. E.g:

Value1 = 'market'
Value2 = '85'
Value3 = 'method 2'

Both instances would be recognized as strings by Python.

ESCAPING:

Let's assume we want to write a string that contains quotation marks already. For example, we want to write this: Oprah Winfrey once said, "Turn your wounds into wisdom". From the basic knowledge of strings, it should probably be written like this: quote = "Oprah Winfrey once said, 'turn your wounds into wisdom'"

However, this would return as an error or as I like to say it, "this would confuse Python's brain" because, python would only read Oprah Winfrey once said, * as strings due to the quotation marks surrounding them. The remaining words would not be read as strings or any other data type in fact and this would cause an error in Python because Python expects only two quotation marks in a string. The best way to prevent this is by escaping.

A common method of escaping is by introducing a backslash () before the quotes of Oprah Winfrey. Therefore, the above code is better written like this: quote = "Oprah Winfrey once said, \" turn your wounds into wisdom\""

The last two quotation marks signify the end of Oprah's quote and the end of the string respectively. This means that the first backslash should be written immediately before the first quote and the second backslash should be introduced immediately before the end of the quote (hopefully, this makes sense)

An alternative (and less stressful) method is to make use of single quotes instead of double quotes. Like this: quote = "Oprah Winfrey once said, 'turn your wounds into wisdom'". Easy peasy isn't it? Well, this could also be done in a reverse manner if we use single quotes for our strings. Like this: quote = 'Oprah Winfrey once said, "turn your wounds into wisdom"'. That's as far as we are going on Strings and Escaping guys. Let's have a look at Numbers now.

NUMBERS (Integers):

As expected, this simply refers to whole-number values or integers. It can be written just the way we write regular numbers and it doesn't have any special requirements, unlike strings. It is important to note that the distinct difference between strings which appear to be numerical and actual numbers are the quotation marks. In simple terms, "75" and 75 are not the same data type. "75" is a string(note the quotes) and 75 is a number (no quotes). A number is simply written like this: num1 = 100.

NUMBERS (FLOATS):

In programming languages like JavaScript, decimals and whole numbers are not separated. They are simply called numbers. Python, however, has an exception to this. Decimal numbers are referred to as floats. Therefore 18.5 is a float and not an integer.

Hopefully, reading this article was worth your time. In the next one, we'll have a look at other data types. You can reach out to me on Twitter Ademola Thompson