In this article, we will learn about sequence types in Python programming language. Sequential data types in Python are data types that are an ordered set which means that the order in which we read values from sequential data types will be the same as the order in which we add values in these data types.
This is the sixth article in the Series: Python Tutorial – Learn Python Programming for Beginners
In our last article, we looked at basic data types in Python programming. Let’s now look at Sequence types supported by Python language.
We will cover some basic sequence types in Python like lists & tuples and also look at some additional sequence types like string, byte & bytearray.
Table of Contents
Sequence Types in Python Programming

Sequence types in Python can be used to store multiple values in an organized fashion. These types are also designed for efficiency to handle multiple values when it comes to adding and reading values. i.e. The order in which we put them in the type is the order in which we get them out from type.
String
The string is one of the most popular data types in any programming language. Python string can be defined as a collection of characters.
Python string is created by enclosing the sequence of characters in the quotes. Python supports single, double & triple quotes for string assignments. The triple quote is used to assign a multiline string to a variable in Python.
Python does not support separate character (char) type so a string with a single character assigned to it can be used for char type in Python.
You can create a string simply by assigning value to a variable.
variable1 = 'Hello World' variable2 = "Hello Python world" variable3 = '''Hello, Pro Code Guide'''
If you will check the type of the above variables using the type() function then for all 3 variables you will get the type as str i.e. <class, ‘str’>
String Index or String as Arrays
Each character in a string is encoded in the ASCII or Unicode character. So we can even say that strings are an array of Unicode characters. Python strings are like arrays with an index starting from 0. Each character in a string can be accessed using these indexes in square brackets as shown below.
variable1 = 'Hello World' print(variable1[0]) print(variable1[1]) print(variable1[2]) print(variable1[3]) print(variable1[4])
The above code on execution will give below output
H
e
l
l
o
String | H | e | l | l | o | W | o | r | l | d | |
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Looping String
Since the string is a collection of characters i.e. arrays we can even loop through string characters using a for loop as shown in the below code
variable1 = 'Hello World' for eachChar in variable1: print(eachChar)
The above code will print each character in the string variable1 (Hello World) on a new line. Run this code and check output as you will notice that space between Hello & World is also a character in string array that gets printed on a separate line.

Check String Length
It is possible to determine the number of characters in a string in Python using the len() function. This len() function takes a string as input and returns the length of that string.
variable1 = 'Hello World' print(len(variable1))
On execution above code will print 11 which is the length or number of characters for string variable1
Modify String
We cannot change the string partially in an attempt to replace some characters in the string we actually have to assign it to a new string altogether. To change the string we completely reassign it to a new string as shown in the code below
variable1 = 'Hello World' print(variable1) variable1 = 'Hello Pro Code Guide' print(variable1)
Below is the output of the above code
Hello World
Hello Pro Code Guide
Delete String
As we saw above that we cannot partially modify strings the same way we cannot delete partial strings but we can delete the entire string in Python by using the del command keyword as shown below
variable1 = 'Hello World' print(variable1) del variable1 print(variable1)
Below is the output of the above code where we are trying to print a variable after it has been deleted.
Hello World
Traceback (most recent call last):
File "c:\ProCodeGuide\Python\sources\SequenceTypes.py", line 4, in <module>
print(variable1)
NameError: name 'variable1' is not defined
bytes
The byte belongs to the binary data type and is used to manipulate binary data in Python. The byte in Python is immutable i.e. they cannot be modified after assignment. The byte in Python stores a sequence of binary values ranging from 0-255 i.e. an 8-bit value.
The byte is a sequence of bytes where each element is a byte.
variable1 = bytes(4) variable2 = bytes(b'Hello World!') print(type(variable1)) print(type(variable2)) print(variable1) print(variable2) print(variable1[1]) print(variable2[1]) variable2[2] = 25
Below is the output of the above code where we have created variable1 and assigned it to empty bytes. Variable2 has been created and assigned a string of bytes. Also, we are trying to modify an immutable byte which throws an error.
<class 'bytes'>
<class 'bytes'>
b'\x00\x00\x00\x00'
b'Hello World!'
0
101
Traceback (most recent call last):
File "c:\ProCodeGuide\Python\sources\SequenceTypes.py", line 13, in <module>
variable2[2] = 25
TypeError: 'bytes' object does not support item assignment
bytearray
The bytearray is the alike list data type and each element is bytearray is a byte i.e. in sort bytearray is an array of bytes. The bytearray is a list of the sequence of binary values ranging from 0-255
A bytearray type is a mutable object. With bytearray being mutable you can modify the value of bytearray.
variable1 = bytearray(b'Hello World!') print(variable1) variable1[0] = 255 variable1.append(255) print(variable1) variable2 = bytes(variable1) print(variable2)
On execution of the above code, we get below output where we have created bytearray and assigned it to a string of bytes. We have tried to change a byte in bytearray and it was successful as bytearray is mutable.
bytearray(b'Hello World!')
bytearray(b'\xffello World!\xff')
b'\xffello World!\xff'
So far we have learned string, bytes & bytearray now let’s look at basic sequence types in Python i.e. list & tuple
List
The list is one of the basic sequence types in Python. The list is the collection-based data type and can be used to store multiple values in a single variable. Lists are just like arrays that can store ordered sets of data values. This collection of data values in the list need not be of the same type.
Data values within the list can be repeated that is duplicate values are allowed in the list and also the list is mutable data type i.e. it can be modified.
There is no special keyword to create a list it can be created by just placing a collection of data values separated by commas inside the square brackets.
variable1 = ['Pro', 'Code', 'Guide', 'Support'] variable2 = ['Pro', 1, 12.5, True, 8e2, 9+3j] print(type(variable1)) print(type(variable2)) print(variable1) print(variable2)
Below is the output of the above code where we have created a variable and assigned a list of strings and a list of data values of different data types. We have then printed the type and value of those variables.
<class 'list'>
<class 'list'>
['Pro', 'Code', 'Guide', 'Support']
['Pro', 1, 12.5, True, 800.0, (9+3j)]
List items are also indexed starting with 0 and can be access with it index in square brackets. The len() function also works on the list and return the number of data items in the list.
variable1 = ['Pro', 'Code', 'Guide', 'Support'] variable2 = ['Pro', 1, 12.5, True, 8e2, 9+3j] print(variable1) print(variable2) print(len(variable1)) print(len(variable2)) print(variable1[1]) print(variable2[2]) variable1[1] = 12.5 variable2[2] = 'Code' print(variable1) print(variable2)
The output of the above code is shown below where we have declared a couple of lists to check their length using len() function, access data items using index, and also modified list as lists are mutable.
['Pro', 'Code', 'Guide', 'Support']
['Pro', 1, 12.5, True, 800.0, (9+3j)]
4
6
Code
12.5
['Pro', 12.5, 'Guide', 'Support']
['Pro', 1, 'Code', True, 800.0, (9+3j)]
List Constructor
A list can also be created using function list() which is also known as a list constructor. This function can also be used to convert compatible data types like Tuple to list.
In the below code, we have created a list using list() constructor and then we have created a tuple which is converted to list using list constructor list().
variable1 = list(('Pro','Code','Guide','Support')) variable2 = ('a', 'e', 'i', 'o', 'u') #Tuple print(list(variable1)) print(list(variable2))
Below is the output of the above code which prints both Lists which are created by using function list()
['Pro', 'Code', 'Guide', 'Support']
['a', 'e', 'i', 'o', 'u']
Tuple
A tuple is one of the basic sequence types in Python. Like list Tuple is also a collection based data type and can be used to store multiple values in a single variable.
Tuples are just like arrays that can store an ordered sets of data values. This collection of data values in the list need not be of the same type.
Data values within the Tuple can be repeated that is duplicate values are allowed in a tuple and also unlike list Tuple is an immutable data type i.e. it cannot be modified.
There is no special keyword to create a tuple it can be created by just placing a collection of data values separated by commas inside the round brackets.
In the below code, we have created a couple of variables and assigned them with a collection of data items i.e. tuple. After the assigned tuple, we have printed the type & value of each variable.
variable1 = ('Pro', 'Code', 'Guide', 'Support') variable2 = ('Pro', 1, 12.5, True, 8e2, 9+3j) print(type(variable1)) print(type(variable2)) print(variable1) print(variable2)
Below is the output of the above code we can see that the type of variables is a tuple which is printed as <class ‘tuple’>.
<class 'tuple'>
<class 'tuple'>
('Pro', 'Code', 'Guide', 'Support')
('Pro', 1, 12.5, True, 800.0, (9+3j))
Tuple items are also indexed starting with 0 and can be accessed with their index value in square brackets. The len() function works on the tuple as well and returns the number of data items in the tuple.
Here is the sample code where we have created a couple of variables and assigned them tuple collection. We have to check the length of tuple data types using len() functions and also printed the value of individual data items using an index with square brackets. We have also tried to modify one of the tuples which are not possible as tuples are immutable.
variable1 = ('Pro', 'Code', 'Guide', 'Support') variable2 = ('Pro', 1, 12.5, True, 8e2, 9+3j) print(variable1) print(variable2) print(len(variable1)) print(len(variable2)) print(variable1[1]) print(variable2[2]) variable1[1] = 12.5
The output of the above code is shown below where we can see that error is thrown as we attempt to modify the tuple data item.
('Pro', 'Code', 'Guide', 'Support')
('Pro', 1, 12.5, True, 800.0, (9+3j))
4
6
Code
12.5
Traceback (most recent call last):
File "c:\ProCodeGuide\Python\sources\SequenceTypes.py", line 13, in <module>
variable1[1] = 12.5
TypeError: 'tuple' object does not support item assignment
Tuple Constructor
Tuple can also be created using function tuple() which is also known as tuple constructor. This function can also be used to convert compatible data types like lists to tuples.
In the below code, we have created a tuple using a tuple() constructor and then we have created a list that is converted to a tuple using a tuple constructor.
variable1 = tuple(('Pro','Code','Guide','Support')) variable2 = ['a', 'e', 'i', 'o', 'u'] #List print(variable1) print(tuple(variable2))
After running the above code we get the below output where we can see that both the variables are printed as tuples.
('Pro', 'Code', 'Guide', 'Support')
('a', 'e', 'i', 'o', 'u')
Summary
In this article, we learned about Sequence types in Python i.e. string, bytes, bytearray, list & tuple. We got a good idea of how these data types are created, accessed, modified & used in the program.
Sequence types in Python are the most popular and frequently used types in Python applications. As part of this series, I will also try to cover in detail the operations that can be performed over these sequence types in Python.
In our next article, we will learn about loops & conditionals statements in the Python programming language.
Please provide your suggestions & questions in the comments section below
References – Sequence Types in Python
Hope you found this article useful. Please support the Author
