Sequence Data Types in Python

Sequence data types are used to store data in containers. They usually contain more than one value. We can access elements of a sequence data type by indexing. String, list, and tuple are the different types of containers used to store the data in a sequential manner.

String:

A string is a sequence of characters enclosed in single, double, or triple quotation marks. While in most cases single and double quotation marks are interchangeable. Multi-line strings are denoted by triple quotation marks. They are immutable, which means that their values cannot be changed once assigned. String data types are commonly used for text processing, I/O operations, and data manipulation.

It is represented by the str class.

  • Python program to demonstrate strings.
  • # String with single quotes

x = ‘Welcome to the Python programming.’

print(x) print(“The data type of x:”, type(x))

print()

Result: x = Scaler

The data type of x: <class ‘str’>

  • # String with double quotes

y = “We’re Muslims.”

print(y)

print(“The data type of y:”, type(y))

print()

Result: y = Scaler

The data type of y: <class ‘str’>

  • # String with triple quotes (Multi-line strings)

z = ‘’’Triple quotes are used 

       for multi-line strings’’’

print(z)

print(“The data type of z:”, type(z))

print()

Result: z = Triple quotes are used 

       for multi-line strings

The data type of z: <class ‘str’>

Accessing elements of a string:

Any character of a string can be accessed using indexing. Python also allows us to use negative indexing to access characters even from the back of a string.

Indexing of a sequence starts from 0.

  • Python program to access characters of a string.
  • x = “Python Programming”

print(x)

  • # Printing first character of the string.

print(“First character:”, x[0])

  • # Printing last character of the string.

print(“Last character:”, x[-1])

  • Result:

Python Programming

First character: P

Last character: g

In the example above, we are printing the first character of the string using the 0th index and the last character of the string using the -1th index.

List:

A list is an ordered collection of similar or different types of items separated by commas and enclosed within brackets [ ]. Lists are mutable, which means that their values can be changed once assigned.

For example,

  • languages = [“C++”, “Java”, “Python”]

print(languages)

Result: [‘C++’, ‘JAVA’, ‘Python’]

  • names = [“Anas”, “Usman”, “Ali”]

print(names)

Result: [‘Anas’, ‘Usman’, ‘Ali’]

  • list1 = [25, 30, “Class”, 5, ‘A’]

print(list1)

print(type(list1))

Result: [25, 30, ‘Class’, 5, ‘A’]

<class ‘list’>

Accessing elements of a list:

A list’s components can be accessed by using their index numbers; use negative indexes to get at the items at the end of the list.

  • # Python program to access elements of a list
  • my_list = [50, 22, ‘Python’, 34, ‘B’]
  • print(my_list)
  • # Accessing first element of the list.
  • print(“First element of the list:”, my_list[0])
  • # Accessing 4th element of the list.
  • print(“4th element of the list:”, my_list[3])
  • # Accessing last element of the list.

print(“Last element of the list:”, my_list[-1])

  • Output:

[1, 10, ‘Python’, 4, ‘A’]

First element of the list: 50

4th element of the list: 34

Last element of the list: B

In the previous example, The first element of the list is accessed by referring to its index “0”,  then the fourth element is accessed by referring to its index “3” and the last element of the list is accessed by using negative indexing.

Tuple:

Tuples are an ordered sequence of one or more types of values, similar to lists, but because they are immutable, their values cannot be changed. They are represented by the tuple class. 

In Python, we use the parentheses () to store items of a tuple. 

For example,

  • product = (‘Xbox’, 499.99)

print(product)

print(type(product))

Result:

(‘Xbox’, 499.99) 

Here, product is a tuple with a string value Xbox and integer value 499.99.

  • my_tuple = (20, 10, ‘Pakistan’, 9, 6)

print(my_tuple)

print(“Its data type is”, type(my_tuple))

  • Output:

(20, 10, ‘Pakistan’, 9, 6)

Its data type is <class ‘tuple’>

Accessing elements of a tuple:

Elements of a tuple can be accessed using their index numbers, while negative indexes are used to access tuple items from the back.

  • # Python program to access elements of a tuple.
  •  x = (2, 32, “Punjab”, 6, ‘Lahore’)

print(x)

  • # Accessing first element of the tuple.

print(“First element of the tuple:”, [0])

  • # Accessing 3rd element of the tuple.

print(“3rd element of the tuple:”, [2])

  • # Accessing last element of the tuple.

print(“Last element of the tuple:”, [-1])

  • Output:

(2, 32, ‘Punjab’, 6, ‘Lahore’)

First element of the tuple: 2

3rd element of the tuple: 32

Last element of the tuple: Lahore

In the above example, we are accessing the elements of a tuple using indexing. At first, we are printing the first element using the 0th index, then we are printing its third element using the 2nd index, at last, we are printing its last element using negative indexing.

Visited 4 times, 1 visit(s) today