Python Tutorial: Working with Lists, Tuples, and Strings in Python

Introduction to Python

Python is a widely used Programming language introduced by Guido van Rossum in 1991. Python supports various libraries for different applications like machine learning, game development, web applications, etc. The easy syntax and readable code of Python programming make this programming language beginner-friendly friendly and it also plays a very important role in software development. 

Python is a platform-independent programming language as it supports the core concept of OOP, which includes classes and Objects.

A class is a template or blueprint for creating Objects, and an Object is an instance of a class, defining the attributes(variables) and behaviour(functions) of a class in a program. A keyword class is used for creating a class in a program, and an object defines the properties and functionality of a program.

Data Types in Python Programming

Data types in Python are used to represent or categorise the kind of data that will be used in program. It gives an idea of what kind of operations will be performed on data in a given program. There are different data types used for various categories of data. 

For numeric values numeric data type will be used. For example, 7.23, -98, 62 are categorised into numeric data types, whereas “Noida”, “Delhi”, “Tpoint Tech Website” are classified into string data types. Given below is the description of Sequence Data Types in Python

Sequence Data Types

The Sequence data type is used to represent an ordered collection of similar or different data types in Python. It is divided into three categories which String, List, and Tuple. It also gives an idea of a particular kind of data comes in which category. Given below is the description of different kinds of data types in Python. 

1. Python String

A String basically contains a message or collection of alphabets separated by space and enclosed within double quotes. 

Example: txt = “Welcome to Tpointtech”

In the above example, txt is a String data type that stores the data in a string format. The String data is always enclosed within the quotes. There is no particular keyword in Python to declare the string data type. 

Programe 1: Python program to demonstrate the operation on string data

msg = “There is raining outside”

print(msg)                   # display the message on the screen

print(len(msg))           # display the length of string on screen

print(msg.upper())    # display the string in upper case

print(msg[9])             # display the character present in the 9th index

 

Output:

There is raining outside

24

THERE IS RAINING OUTSIDE

r

 

  

Program 2: Python program to check whether a string is palindrome or not

# A palindrome is a string that reads the same both forward and backwards, ignoring the case and alphanumeric characters

def _bool_is_palindrome(msg): #function to check Palindrome

    left = 0   # index points to the starting index of the string

    right = len(msg) - 1  # index points to the last index of a string

 

   

    while left < right:   #run a while loop in a string

     

        # If the characters at the current positions

        # are not equal

        if msg[left] != msg[right]:

            return False

 

        left += 1   # move the pointer forward 

        right -= 1  # move the pointer backwards to check with the

                             # corresponding left character

    return True # if no mismatch is found, it means the string is a plaindrome

# driver code

msg = "racecar"

print(_bool_is_palindrome(msg))

   

Output:

True

 

  1. Python List

Python lists are used to store a collection of data, just like an array, but lists are much flexible as compared to arrays as you can store only one data type in an array, either int or string data type at a time but in a list, you can store multiple data types in a single list at a time. A list in Python will be created by placing items inside the square brackets. Given below are examples of lists in Python.

Example: Pyhthon program to demonstrate the working of List in Python

item1 = []

# list only containing the integer value

item1 = [7, 8, 9]

print(item1)

# list containing both integer and string data types

item2 = ['Tpoint', 'Tech', 'Website', 0, 1,  2]

print(item2)

 

Output:

[7, 8, 9]

[“Tpoint”, “Tech”, “Website”,0, 1, 2]

 

3. Python Tuples

The function of Tuples in Python is mostly similar to List. The difference between List and Tuple in Python is that data stored in a list can be change once it is created, whereas data stored in a Tuple can not be change or modified once it is created. Tuples are used in Python when the programmer wants to keep the data items permanent and doesn’t want to modify them in the future. 

Tuples are known for their immutability only which plays a key role in various applications. Similar to a list, a Tuple can also contain data elements of any data type. Given below is an example of a tuple in Python

Example

// Python program to demonstrate the working of tuple

tuple1 = (“Monkey”, 12.6,  78, “Animal”)

print(tuple1)

Output

('Monkey', 12.6, 78, 'Animal')

           

Conclusion

Python Programming language is in high demand in the present time because of its various features and it will be in demand in future as well. I hope this article has provided you a valuable Information about Lists, Tuple, and Strings in Python Programming

If you are looking for more of  such type of information in Programming, you can visit the Tpoint tech website where,  you can get valuable information of different Programming languages with examples and an online compiler as well.