Colon In Python – Why It Is Used?

In Python, a colon (:) is very important. A colon in Python is used for a variety of purposes, including function declaration, data retrieval, array indexing, and more. Let’s go over the functions and applications of colons in more detail below.

What the colon (:) is used for?

Below are the various reasons for which colon in Python is used.

  • An indented block is represented by the use of a colon.
  • It can also be used to retrieve data and index ranges or arrays of values.
  • Slicing is yet another important application for the colon. If the programmer wants to slice a data set, he or she must first specify the starting index followed by the ending index, which is separated by a colon, which is the general syntax of cutting.
  • In dictionaries, the keys are identified by the use of a colon.

Many other Python functions make use of the colon, and we’ll demonstrate how to use them in practice in the functions below.

Types Of Colon In Python

There are basically two types of colon used in Python.

  • Singel Colon (:).
  • Double Colon (::).

Usage Of Colon For Indentation In Python

In the example below, you can see that when you use a colon at the end of a line, the next line is automatically indented.

#Initializing a Number
num = 15

#If Num is 15 then use Colon
if num==15:
    print("The number is 15.")
else:
    print("Mismatch.")
The number is 15.

Using Colon In Python For String Slicing

The colon operator is used in slicing to perform a variety of tasks, including indexing a specific range and displaying the output.

#Initializing a String
inputString = "You Are On Coduber"

#Printing Coduber using Slicing
cutString = inputString[-7:]

print(cutString)

Output:

Coduber
Colon In Python

By placing the index on the right side of the colon, everything after that specific index will be displayed as output. The index that is mentioned in the code will not be displayed in this situation.

#Initializing a String
inputString = "You Are On Coduber"

#Printing Coduber using Slicing
cutString = inputString[11:18]

print(cutString)

Output:

Coduber

Let us see another code example for negative index usage with a colon in Python for slicing the string.

#Initializing a String
inputString = "You Are On Coduber"

#Printing Coduber using Slicing
cutString = inputString[-10:-3]

print(cutString)

Output:

On Codu

Using Colon To Access Specific Item In The List

Accessing specific elements from a list works in a manner similar to that which we saw in string slicing earlier. With the help of the index range and the colon operator, a specific set of words or elements will be displayed. If you want a better understanding, consider the following examples.

#Initializing a List of String
inputList = ["CMU", "ASU", "MIT", "CALTECH"]

#Using Colon To Select Specific Item
#From the list
selectItem = inputList[1:2]

print(selectItem)

Output:

['ASU']
#Initializing a List of String
inputList = ["CMU", "ASU", "MIT", "CALTECH"]

#Using Colon To Select Specific Item
#From the list
selectItem = inputList[:3]

print(selectItem)

Output:

['CMU', 'ASU', 'MIT']
#Initializing a List of String
inputList = ["CMU", "ASU", "MIT", "CALTECH"]

#Using Colon To Select Specific Item
#From the list
selectItem = inputList[2:]

print(selectItem)

Output:

['MIT', 'CALTECH']

Using Colon In Python To Identify Key and Value In Dictionary

The colon is used to represent a key-value pair in a Python dictionary. The value to the left of the colon is known as the key, and the value to the right is known as the value.

#Initializing a dictionary
inputDict = {"First Name": "John", "Last Name": "Muller"}

#Printing First name and Last name
#using Colon
print(inputDict)

Output:

{'First Name': 'John', 'Last Name': 'Muller'}
Colon In Python

Wrap Up

As you can see, the colon is extremely important in Python, and it is used for a variety of purposes, ranging from representing a dictionary to slicing a list of values.

Please let me know in the comments section if there is anything I have missed; I would be delighted to include it here.

Further Read:

  1. Iterate Through A List In Python
  2. Exit A Python Program in 3 Ways
  3. How To Find A String In A List In Python
  4. Python 3 Round To 2 Decimal Places: Code Examples

Leave a Comment