Sayantan's Blog On Python Programming

ACCESSING STRING TUPLE IN PYTHON

ACCESSING STRING TUPLE IN PYTHON

INTRODUCTION

String tuple will contain string variable as a field.

ORDER OF PYTHON TOUPLE

Order in Python list is very important. Because tuple fields are accessed in the order they appear in the tuple.

# String tuple example 1

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nElement of tuple ->", str_tuple)

ACCESSING STRING TUPLE IN PYTHON : Output

Element of tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

TOUPLE INDEX

Indexes are used to access the individual field in the tuple. Index in Python tuple starts with 0.

# String tuple example 2

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nElement of tuple ->", str_tuple)

print("\n1st field of tuple str_tuple ->", str_tuple[0])
print("2nd field of tuple str_tuple ->", str_tuple[1])
print("3rd field of tuple str_tuple ->", str_tuple[2])
print("4th field of tuple str_tuple ->", str_tuple[3])
print("5th field of tuple str_tuple ->", str_tuple[4])
print("6th field of tuple str_tuple ->", str_tuple[5])

ACCESSING STRING TUPLE IN PYTHON : Output

Element of tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

1st field of tuple str_tuple -> Python
2nd field of tuple str_tuple -> Java
3rd field of tuple str_tuple -> C++
4th field of tuple str_tuple -> C
5th field of tuple str_tuple -> PHP
6th field of tuple str_tuple -> XML

ACCESSING TUPLE INDEX IN REVERSE ORDER

Python allows negative values as index which in turn helps us to access the tuple fields in reverse order.
When we are accessing the tuple fields from reverse order then index position 0 will not be used as we are using negative index.

# String tuple example 3

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nElement of tuple ->", str_tuple)

print("\n1st field of tuple str_tuple ->", str_tuple[-6])
print("2nd field of tuple str_tuple ->", str_tuple[-5])
print("3rd field of tuple str_tuple ->", str_tuple[-4])
print("4th field of tuple str_tuple ->", str_tuple[-3])
print("5th field of tuple str_tuple ->", str_tuple[-2])
print("6th field of tuple str_tuple ->", str_tuple[-1])

ACCESSING STRING TUPLE IN PYTHON : Output

Element of tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

1st field of tuple str_tuple -> Python
2nd field of tuple str_tuple -> Java
3rd field of tuple str_tuple -> C++
4th field of tuple str_tuple -> C
5th field of tuple str_tuple -> PHP
6th field of tuple str_tuple -> XML

In the above example, we can see that by using negative index values we have printed the tuple in normal order. In below example we will see that if we start with index value -1 then the entire tuple will be printed in reverse order.

# String tuple example 4

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nElement of tuple ->", str_tuple)

print("\n1st field of tuple str_tuple from the end ->", str_tuple[-1])
print("2nd field of tuple str_tuple from the end ->", str_tuple[-2])
print("3rd field of tuple str_tuple from the end ->", str_tuple[-3])
print("4th field of tuple str_tuple from the end ->", str_tuple[-4])
print("5th field of tuple str_tuple from the end ->", str_tuple[-5])
print("6th field of tuple str_tuple from the end ->", str_tuple[-6])

ACCESSING STRING TUPLE IN PYTHON : Output

Element of tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

1st field of tuple str_tuple from the end -> XML
2nd field of tuple str_tuple from the end -> PHP
3rd field of tuple str_tuple from the end -> C
4th field of tuple str_tuple from the end -> C++
5th field of tuple str_tuple from the end -> Java
6th field of tuple str_tuple from the end -> Python

If we try to access a field of tuple which does not exists, then Python will raise an error -> “tuple index out of range

# String tuple example 5

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nElement of tuple ->", str_tuple)

print("\n1st field of tuple str_tuple ->", str_tuple[0])
print("2nd field of tuple str_tuple ->", str_tuple[1])
print("3rd field of tuple str_tuple ->", str_tuple[2])
print("4th field of tuple str_tuple ->", str_tuple[3])
print("5th field of tuple str_tuple ->", str_tuple[4])
print("6th field of tuple str_tuple ->", str_tuple[5])

print("7th field of tuple str_tuple ->", str_tuple[6])

ACCESSING STRING TUPLE IN PYTHON : Output

Element of tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

1st field of tuple str_tuple -> Python
2nd field of tuple str_tuple -> Java
3rd field of tuple str_tuple -> C++
4th field of tuple str_tuple -> C
5th field of tuple str_tuple -> PHP
6th field of tuple str_tuple -> XML
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\01_hello.py", line 17, in <module>
    print("7th field of tuple str_tuple ->", str_tuple[6])
                                             ~~~~~~~~~^^^
IndexError: tuple index out of range

In the above example, we have tried to access the 7th field of the tuple which doesn’t exists. So python has raised the error.

len() FUNCTION

len() function helps us to count the string fields present in the tuple including duplicates.

# String tuple example 6

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python') 
print("\nElement of tuple ->", str_tuple)

print("\nNumber of element present in the tuple str_tuple ->", len(str_tuple))

ACCESSING STRING TUPLE IN PYTHON : Output

Element of tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python')

Number of element present in the tuple str_tuple -> 7

count() FUNCTION

count() function returns the number of occurrences of a field in a tuple. If any field is not present in the tuple then count() function will return 0.

# String tuple example 7

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python', 'PHP') 
print("\nElement of tuple ->", str_tuple)
print("\nNumber of occurrence of Python in the tuple str_tuple ->", str_tuple.count('Python'))
print("\nNumber of occurrence of PHP in the tuple str_tuple ->", str_tuple.count('PHP'))
print("\nNumber of occurrence of Word in the tuple str_tuple ->", str_tuple.count('Word'))

ACCESSING STRING TUPLE IN PYTHON : Output

Element of tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python', 'PHP')

Number of occurrence of Python in the tuple str_tuple -> 2

Number of occurrence of PHP in the tuple str_tuple -> 2

Number of occurrence of Word in the tuple str_tuple -> 0

set() FUNCTION

set() function removes the duplicate fields from the tuple.

# String tuple example 8

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python', 'PHP') 
print("\nElement of tuple ->", str_tuple)
print("\nElement of tuple after removing duplicates ->", set(str_tuple))

ACCESSING STRING TUPLE IN PYTHON : Output

Element of tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python', 'PHP')

Element of tuple after removing duplicates -> {'Java', 'PHP', 'C++', 'C', 'XML', 'Python'}

In the above example, we can see that the original tuple contains “Python“, “PHP” twice. But after using the set() function, the duplicates have been removed. The revised tuple contains only one field for “Python“, “PHP“. Also the output is enclosed with curly brackets which indicates the output is a set.

ACCESSING STRING TUPLE IN PYTHON : Output
ACCESSING STRING TUPLE IN PYTHON : Output

RELATED TOPICS:

1 thought on “ACCESSING STRING TUPLE IN PYTHON”

  1. Pingback: IMMUTABILITY OF TUPLE IN PYTHON - Sayantan's Blog On Python Programming

Leave a Comment

Your email address will not be published. Required fields are marked *