## DEV Community Introschool

Posted on • Updated on

# Data Types In Python

Subscribe to our Youtube Channel To Learn Free Python Course and More

Basic Data Types in Python
In Python, every data item is an object. We know that every object has certain characteristics. Type is one such characteristic of an object. Every data is of a different type. Basic data types are Number, String, and Boolean.

In this section, we will discuss these basic data types in detail. You already know a little bit about them, so let's explore them in detail.

Numbers
Number data types are used to store numeric values. Numbers are immutable data types, it means that if you can not change its value if you change the value, the interpreter will create new number object. Python supports integer, floating point, and complex numeric values.

Integer(int) - Integers include positive and negative whole numbers but they don’t include fractions or decimal. There is no limit to the length of the integer, the only limit is the memory of the computer.

Floating Point(float) - The Floating point numbers are a numeric value with the decimal point.

Complex - The complex numbers are represented as a + bj form. Here a is real part and b is an imaginary part.
You can use Python’s built-in functions type() to know the type of a variable. Let’s see the below examples.

``````# Number Datatypes in Python

a = 2384735738437957379835798

b = 123

c = 1.0

d = 4 + 5j

# Let's check the type of above variables.
type(a)
# Output <class 'int'>

type(b)
# Output <class 'int'>

type(c)
# Output <class 'float'>

type(d)
# Output <class 'complex'>
``````

Here you see the or . Classes are part of Object Oriented Programming, which is an advanced concept, but for your understanding, think of classes as a factory which makes objects.
So when the type function gives output it means that particular variable refers to the integer object type.

Strings
Strings are the sequence of characters. Characters are anything that you can type from the keyboard. Even space is also a character. Python interpreter considers anything string if it is inside a single quote(‘’) or double quote(“”). The string type is called str in Python. Strings are immutable, you can not change it once you create it.

You can use type() function to check the type of a variable.

``````# Strings
a = 'Hello'
b = "Python is awesome"

type(a)
# Output: <class 'str'>

type(b)
# Output: <class 'str'>
``````

How to get the length of the String?
Length of any string is the number of character present in the string. To get the length of the string, Python’s built-in function len() is used.

``````# Length of String

s = "Hello World"
print(len(s))

# Output: 11
``````

How to Access Character in the String
For accessing the character in the string, you can use the index of each character. String index starts from 0 and keeps on increasing till the last character of the string. Python also allows negative indexing. Negative indexing starts from the last character with the index -1 and keeps on decreasing till the first character. Let’s see how to access string characters.

``````s = "Python"

s1 = s
s2 = s
print('s1: ', s1)
print('s2: ', s2)

#Output
s1: 'P'
s2: 'O'
``````

You can also check by comparison operator(==) to check the string characters.

``````s == s[-6]
# Output: True

s[-1] == s
# Output: True
``````

How to slice the String
Slicing means taking out the substring from the actual string. To get the substring, you can use the expression s[start:stop]. Here start and stop are the indexes of the string. The start is the index from where you want to start the substring and stop is the index where you want to end your substring. The character at index stop will not be included in the substring. See the below code.

``````# String Slicing
s = 'String'

str1 = s[0:3]

str2 = s[2:4]

print('str1:', str1)
print('str2:', str2)

# Output
str1: 'Str'
str2: 'ri'
``````

See the below picture. In picture (a), substring s[0:3] starts from index 0 and extends up to index 2, it doesn’t include index 3. In picture (b), substring s[2:4] starts from index 2 and extends up to index 3, substring doesn’t include index 4. If you remove the first index, slicing will start from the beginning of the string and similarly, if you remove the last index, slicing will extend till the end of the string.

``````str3 = s[:4]
str4 = s[3:]
str5 = s[:]

print('str3:', str3)
print('str4:', str4)
print('str5:', str5)

# Output
str3: 'Stri'
str4: 'ing'
str5: 'String'
``````

You can also give the step index in the syntax s[start: stop: step] to provide steps in the sliced string. If you don’t specify the step index then its default value is 1. See the below code.

``````# String slicing with step index
s = 'String Slicing'
str1 = s[2:len(s): 2]
print(str1)

# Output: rn lcn
``````

String Operations
Using ‘+’ Operator
You can combine two or multiple strings using the ‘+’ operator.

``````first_name = 'John'
second_name = 'Snow'

full_name = first_name + ' ' + second_name
print(full_name)

# Output
'John Snow'
``````

Using ‘*’ Operator
You can multiply string with the integer to create a repetitive sequence.

``````a = 'Hello'

b = s * 3
print(b)

#Output
'HelloHelloHello'
``````

Using ‘in’ Operator’
With the help of in operator, you can check whether one string is a substring of other string or not.

``````a = 'I am learning Python'
b = 'Python'
c = 'hello'

print(b in a)
print(c in a)
# Output
True
False
``````

How to format the String
str.format Method
String formatting is all about placing variables in the string. See the below code.

``````name = 'Ramesh'
print('Hello {}'.format(name))

# Output
Hello Ramesh
``````

In the above code, you can see that you can use curly braces {} at the place of variable and inside format method, you can write the variable name.
If you have more than one variable then

``````name = 'Ramesh'
language = 'Python'

print('I am {} and I am learning {}.'.format(name, language)

# Output
# I am Ramesh and I am learning Python
``````

f-strings
f-string is the new feature in Python. It is the very simplest way of formatting string.

``````# f-string syntax

name = 'Ramesh'
print(f'Hello {name}')
# output
# Hello Ramesh
``````

More examples

``````fruit = 'mangoes'
quantity  = 20

print(f'I love {fruit}, I can eat {quantity} {fruit} in a day.')
# Output
# I love mangoes and I can eat 20 mangoes in a day.
``````

Escape Sequence
If you are asked to write this string - I’m a software developer, then how would you write it in Python. Escape sequence allows you to write special characters in the string. There are many ways of writing it. See the below code.

``````# Using double quotes
s1 = "I'm a software developer"
print(s1)
# Output: I'm a software developer

# Using escape sequence backslash(\)
print('We are using '\single quotes\' in the string'.)
# Output: We are using 'single quotes in the string.

# print newline character.
print('Hello world!\nWe are learning \'Python\'.')

# Output:
# Hello world!
# We are learning 'Python'.

'''
Here the output is not like - Hello World! We are learning 'Python'.
The reason is we used escape sequence newline(\n), so the interpreter knows that it's a newline character.
'''
``````

You can see the complete list of escape sequences here.
Boolean
Boolean type objects have only two values, True or False. You can use type() function to check the data type of these values.

``````# Boolean Data Type

type(True)
# Output: <class 'bool'>

type(False)
# Output: <class 'bool'>
``````

As we have discussed before, values other than True or False, also have boolean context. You can check it by Python’s bool() function. The bool() function return True for truthy values and False for falsy values.

``````# Boolean context of other values

bool(1)
# Output: True

bool(0.0)
# Output: False

bool('')
# Output: False
`````` 