The Python Strings, Lists & Tuples

Strings:
Python strings are the collection of characters followed by single quotes, double quotes, or triple quotes. Strings can be created by enclosing the character or the sequence of characters in the quotes. Python allows us to use single quotes, double quotes, and triple quotes to create the strings.

Consider the following example to create a String.

Code:
#Using single quotes
str1='TechieMania'
print(str1)

#Using double quotes
str2="TechieMania"
print(str2)

#Using triple quotes
str3="'Triple quotes are generally used to represent the multiline or docstring"'
print(str3)

Output:
TechieMania
TechieMania
Triple quotes are generally used for representing the multiline or docstring

Strings indexing
Same like other languages, the indexing of the Python string starts from 0. For example,
                  str="TECHIE"
str[0]='T'
str[1]='E'
str[2]='C'
str[3]='H'
str[4]='I'
str[5]='E'

Consider the following example:
str="TECHIE"
print(str[0])
print(str[1])
print(str[2])
print(str[3])
print(str[4])
print(str[5])

#It returns IndexError because 7th index does not exist
print(str[7])

Output:
T
E
C
H
I
E
IndexError: string index out of range

Reassigning Strings:
Updating the content of the strings is as easy as assigning it to a new string. The string object does not support item assignment. Strings are immutable in Python.

Consider the following example:
str =" TECHIE"
print(str)
str ="techie"
print(str)

Output:
TECHIE
techie

Deleting the String:
As we know that strings are immutable. We cannot delete or remove the characters from the string.  But we can delete the entire string using the del keyword.

Consider the following example:
Code:
str = "TECHIEMANIA"
del str
print(str)

Output:
NameError: name 'str' is not defined


Lists:
A list is used to store the sequence of various types of data. Python lists are mutable type it is mean we can modify its elements after it created. Python consists of six data-types that are capable to store the sequence, but the most common are reliable type is the list.

A list can be defined as a collection of values or items of different types.
The items in the list are separated with comma(,) and enclosed with the square brackets [].

Characteristics of Lists


  • The lists are ordered.
  • The element of the list can access by index.
  • The lists are the mutable type.
  • A list can store the number of various elements.
List Indexing:
          The indexing is processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator [].

The index starts from 0 and goes to length -1. The first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.

                           List = [0,1,2,3,4,5]
List[0] = 0
List[1] = 1
List[2] = 2
List[3] = 3
List[4] = 4
List[5] = 5

We can get the sub-list of the list using the following syntax:
list_variable(start:stop:step)
  • The start denotes the starting index position of the list.
  • The stop denotes the last index position of the list.
  • The step is used to skip the nth element within a start: stop.
Updating the List Values:
        The list is the most versatile data structures in Python since they are mutable, and their values can be updated by using the slice and assignment operator.

Python also provides append() and insert() methods, which can be used to add values to the list.
Consider the following example:

list = [1,2,3,4,5,6]
print(list)

list[2] = 10
print(list)

Output:
[1,2,3,4,5,6]
[1,2,10,4,5,6]

Python List Operations:
      The concatenation (+) and repetition (*) operators work in the same way as they were working with the strings.

Repetition: The repetition operator enables the list elements to be repeated multiple times.

Concatenation: It concatenates the list mentioned on either side of the operator.

Membership: It returns true if a particular item exists in a particular list otherwise false.

Iteration: The for loop is used to iterate over the list elements.

Length: It is used to get the length of the list.


Tuples:
          Python Tuple is used to store the sequence of immutable Python objects. The tuple is similar to lists since the value of the items stored in the list can be changed, whereas the tuple is immutable, and the value of the items stored in the tuple cannot be changed.

A tuple can be written as the collection of comma-separated(,) values enclosed with the small(0 brackets. The parentheses are optional but it is good practice to use.

Where use Tuple?
  1. Using tuple instead of last gives us a clear idea that tuple data is constant and must not be changed.
  2. Tuple can simulate a dictionary without keys. 
Tuple Indexing:
          The indexing in the tuple is similar to lists. The indexing in the tuple starts from 0 and goes to length(tuple) -1.
The item in the tuple can be accessed by using the index [] operator. Python also allows us to use the colon operator to access multiple items in the tuple.

              Tuple = (0,1,2,3,4,5)
Tuple[0] = 0
Tuple[1] = 1
Tuple[2] = 2
Tuple[3] = 3
Tuple[4] = 4
Tuple[5] = 5
Consider the following example:
tup = (1,2,3,4,5,6,7)
print(tup[0])
print(tup[1])
print(tup[8])

Output:
1
2
tuple index out of range

Negative Indexing:
           The tuple element can also access by using negative indexing. The index of -1 denotes the rightmost elements and -2 to the second last item and so on.
The elements from left to right are traversed using the negative indexing.

tuple1 = (1,2,3,4,5)
print(tuple1[-1])
print(tuple1[-4])

Output:
5
2

Deleting Tuple:
          Unlike lists, the tuple items cannot be deleted by using the del keyword as tuples are immutable. To delete an entire tuple, we can use the del keyword with the tuple name.

Consider the following example:

tuple1 = (1,2,3,4,5,6)
print9tuple1)
del tuple1[0]
print(tuple1)
del tuple1
printf(tuple1)

Output:
(1,2,3,4,5,6)
NameError: name 'tuple1; is not defined

Python Tuple inbuilt functions:
  1. CMP(tuple1, tuple2): It compares two tuples and returns true if tuple1 is greater than tuple2 otherwise false.
  2. len(tuple): It calculates the length of the tuple.
  3. max(tuple): It returns the maximum element of the tuple.
  4. min(tuple): It returns the minimum element of the tuple.
  5. tuple(seq): It converts the specified sequence to the tuple.
Hope this found you interesting and for more technical stuff, Stay tuned. 
Thank You.
~Jinal Menpara
     



Comments

Post a Comment