What are the data types? What are the Primitive data types in python?

 

What are the data types?

In computer programming data type refers to the attribute of the data that allows the compiler or an interpreter to understand the intent to use the data. Datatypes are basically used to distinguish the certain properties of multiple objects. In other words, datatypes classify different type of data.

data types in python, string in python, python split, python split string, python lowercase, python string methods, python uppercase, python string, python string format, python

Let’s take a real-life example to understand the need for data types, consider two objects bicycle and motorcycle.  A bicycle does not have an engine or an accelerator but the motorcycle has them both. These are the properties of a motorcycle that distinguish it from a bicycle. Moreover, the operation you can perform using a motorcycle can not be performed on a bicycle. So, the same type of objects will group under a datatype that facilitates their distinction and possible operation that can be performed on the objects under that particular type.

Likewise, we can add, subtract, multiply and divide two numbers but not a number and word.

>>> 2 + 3                   valid

>>> 2 + hello             invalid

That is the reason the numbers are classified in an integer or float data types and words in string datatype. Both have their own defined operation.

Type of Data types in Python:

In python, we have four primitive(built-in) data types:

  1. String
  2. Integer
  3. Float
  4. Boolean

Let’s explore the first data type in detail.

String:

The string data type is used to store sequence of characters. For example, words, characters and alphabets. In python, strings are enclosed either in double or single quotes. This flexibility allows us to use apostrophes and double quotes inside the string. Let’s make it a bit clear with the following example:

 

>>> print(“This is a string”)

>>> print(‘This is also a string’)

>>> print(“This is ‘another’ string”)

>>> print(‘This is a “double quotes” string’)

 

Output:

This is a string

This is also a string

This is ‘another’ string

This is a “double quotes” string

 

Let’s explore some mostly used functions(operations) of strings

title() is a function of string which is used to convert the string to title case(first letter capital of every word in a sentence). Let’s understand this with the following example:

 

>>> name = “john doe”

>>> print(name.title())

 

Output:

John Doe

 

The method title() appears after the variable. A method is an action that Python can perform on a piece of data. The dot (.) after name in name.title() tells Python to make the title() method act on the variable name. As the title() method do not need any extra information so, parenthesis after that are empty.

There are a few more methods of strings:

 

>>> name = “JoHn dOe”

>>> print(name.lower())

>>> print(name.upper())

 

Output:

john doe

JOHN DOE

 

lower() method is useful while storing and matching data once it is retrieved. Especially when the user is free to type data it’s better to save it by converting it to lowercase and then display in an appropriate format. For example, while filling a form user can enter a name in any format but while displaying it to user, we will display it in title format.

Concatenation of multiple strings:

In python, plus(+) sign is used to combine multiple strings. Let’s understand this with the following example:

 

>>> first_name = “John”

>>> last_name = “doe”

>>> full_name = first_name + “ ” + last_name

>>> message= “Hello, ” + full_name.title()  + ”!”

>>> print(message)

 

Output:

Hello, John Doe!

 

 

The method we perform here by combining first and last name and making a full name is called concatenation.

Adding whitespace using escape sequence:

The escape sequences used for adding whitespace within a string are \t and \n.

In programming, whitespace refers to any nonprinting character, such as spaces, tabs, and end-of-line symbols. You can use whitespace to organize your output so it’s easier for users to read. Consider the example below:

 

>>> print(“Programming in python”)

>>> print(“\tProgramming in python”)

 

Output:

Programming in python

       Programming in python

 

\t escape sequence is used in the second line of code to add the tab space before the sentence. Let’s take another example to understand how \n escape sequence works:

 

>>> print(“Python\nJava\nHTML”)

 

Output:

Python

Java

HTML

\n works like an enter. It is used to add a new line. We can use \t and \n together too. Consider the example below:


>>> print(“\tPython\n\tJava\n\tHTML”)

 

Output:

     Python

     Java

     HTML

Newlines and the tab will be very useful for you when you will execute the code whose output is multi-line.

Stripping whitespace:

We can trim the extra spaces in start and end of our string by using the strip() method of string. The variant of it includes rstrip() and lstrip() that removes whitespace from the right and left respectively. To programmer, “Python” and “Python ” may seem same but to program, it is not same and will definitely cause trouble while matching two stings. So, it’s better to trim those whitespaces in a string before comparing it with other strings. Let’s take the example:

 

>>> language = “ python ”

>>> print(language.strip())

>>> print(language.rstrip())

>>> print(language.lstrip())

>>> print(language)

 

Output:

‘python’

‘ python’

‘python ’

‘ python ’

 

Note: Here, single quotes are not part of output these are included just to show the whitespace.

Python split string:

split() is a string method used to separate the string in a list on the basis of provided separator. The default separator for this method is whitespace. Let’s have a couple of examples for it:

 

>>> message = “Welcome to the world of python”

>>> print(message.split())

 

Output:

['Welcome', 'to', 'the', 'world', 'of', 'python']

 

Here, the string is separated in a list on the basis of whitespace. 

 

>>> fruits = “Apple#Strawbery#Oranges#Banana”

>>> print(fruits.split(“#”))

 

Output:

['Apple', 'Strawbery', 'Oranges', 'Banana']

 

Here, the string is separated using hash “#” separator. You can also use a comma and any possible combination.

We can also set the amount of split by adding the maxsplit parameter. Consider the example below:

 

>>> fruits = “Apple#Strawbery#Oranges#Banana”

>>> print(fruits.split(“#”, 1))

 

Output:

['Apple', 'Strawbery#Oranges#Banana']

 

That’s all about string. In the next article, I will discuss the next three data types of python in detail.

If you found this article helpful share it with others. Let me know your queries and suggestions in the comment section below. Do subscribe my site and channel as I’m going to upload a series of articles and videos on programming in python and I don’t want you to miss any update.

Moreover, you can also contact me and share your queries on Instagram, Facebook or Twitter. I would love to provide a solution to your issues.

Stay tuned and strive to learn. Keep learning, keep growing!

Post a Comment

0 Comments