Colon in Python – Why do we use (:) in Python?

Colon In Python

A colon (:) holds a lot of importance in Python. A colon in Python is used for multiple functions including declaring functions, fetching data, array indexing, and more. Let’s discuss the functions and the uses of colons in further detail below.


Functions of the colon(:)

  • A colon is used to represent an indented block.
  • It is also used to fetch data and index ranges or arrays
  • Another major use of the colon is slicing. In slicing, the programmer specifies the starting index and the ending index and separates them using a colon which is the general syntax of slicing.
  • A colon is used to identify the keys in dictionaries.

There are many more uses of the colon in Python and we’ll practically use the functions below.


Types of colons in Python

  • Colon (:)
  • Double colon (::)

Using the Colon in Python for indentation

As you’ll see in the example below, the moment you use a colon on a line, the next line is automatically indented.

>>> a = 20
>>> if a > 10:
         print("20 is greater than 10")
    else:
         print("20 is less than 10")

Output
20 is greater than 10

Using Colon (:) in Strings for slicing

The functions of colon operator in slicing includes indexing a specific range and displaying the output using colon operator.

>>> a = "AskPython"
>>> print(a[2:8])
kPytho

A colon used on the right side of the index will display the everything after that particular index as an output. This will not display the index that is mentioned in the code.

>>> a = "AskPython"
>>> print(a[3:])
Python

A colon used on the left side of the index will display everything before the particular index as an output. This will display that index mentioned in the code too.

>>> a = "AskPython"
>>> print(a[:7])
AskPyth

Negative Indexing : Slicing of index will happen from the end of the string using the colon operator.

>>> a = "AskPython"
>>> print(a[-5:-2])
yth

Using Colon (:) to access specific list elements

Accessing particular elements from the list works in the similar way as we observed in string slicing above. A particular set of words or elements will be displayed with the help of index range and the colon operator. Look at the following examples for a better understanding:

>>> list = ["Python","C","Java","Mysql","PHP","Ruby","HTML"]
>>> print(list[2:6])
['Java','Mysql','PHP','Ruby']
>>> list = ["Python","C","Java","Mysql","PHP","Ruby","HTML"]
>>> print(list[:5])
['Python','C','Java','Mysql','PHP']
>>> list = ["Python","C","Java","Mysql","PHP","Ruby","HTML"]
>>> print(list[2:])
['Java','Mysql','PHP','Ruby','HTML']

A colon operator can also be used for replacing certain elements in the list with existing elements.

>>> list = ["Python","C","Java","Mysql","PHP","Ruby","HTML"]
>>> list[2:4]=["c++","Kotlin"]
print(list)
['Python','C','C++','Kotlin','PHP','Ruby','HTML']

Using colons to identify keys in Dictionaries

Dictionaries are displayed in key : value format.

>>> dict = {
         "StudentName" : "ABC",
         "StudentAge" : "21",
         "Course" : "ComputerScience"
         }
>>> print(dict)
{'StudentName': 'ABC', 'StudentAge': '21', 'Course': 'ComputerScience'}

Double Colons (::) in Python

The double colons (::) in python are used for jumping of elements in multiple axes. It is also a slice operator. Every item of the sequence gets sliced using double colon.

Take for example a string ‘Ask python’ and we’ll try to manipulate it using the slice operator for better understanding. After initializing the variable with specify the index number in the same column and use ::

>>> string = "Ask Python"[5::]
>>> print(string)
ython

These double colons are used to separate the values of the flags from each other. When the programmer does not specify the values of the flags, the interpreter will use its default values.

The syntax of a Slice operator using double colon is [Start : Stop : Steps]. Start (Indicates the number from where the slicing will start), Stop(Indicates the number where the slicing will stop) and Steps(Indicates the number of jumps interpreter will take to slice the string) are the three flags and all these flags are integer values.

>>> string = "What's up AskPython"[0:19:2]
>>> print(string)
Wa' pAkyhn

In the above code, the interpreter after execution printed every second character starting from index 0 to index 19. This code can be reduced to a short cut by using double colon :: operator.

>>> string="What's up AskPython"[::2]
>>> print(string)
Wa' pAkyhn

The :: operator used before the index number will produce the same output.

Conclusion

While the topic is quite small, understanding the use of a colon in Python can help you ease through learning dictionaries, functions, and more.

Stay tuned for further tutorials!