Subject: Python Programming

Field: Computer Science

Written by: duova


Completed: 3,529
Computer Science Requests: 2,938

The 6 Major Types Of Python Literals: Everything You Need To Know

Literals in Python represent raw information assigned to fixed or variable values when programming. Literal is essentially a quick and simple way to define a value. They may be in the form of text, Boolean data, integers, or any other type of data. The value of Python literals doesn’t change as the program runs. 

In this article, we’ll take a closer look at the six major types of literals and provide some useful examples that will help understand the concept better. 

Photo by Artturi Jalli on Unsplash

String Literals in Python

A string literal is created when text is encased in quotations. To specify a string literal, you can use single or double quotes. There are several ways to specify them, which essentially create different types of string literals. As you are teaching yourself how to code, consider the following string literals:

Single Line String Literal

A single line literal is created when strings are only supplied on a single line. Both double quotation marks and single quotation marks can be used to wrap single-line string literals. To get a grasp of how they operate, consider the examples below:


single_quotes_string='Good Morning!'

double_quotes_string="How are you?"


Good Morning!

How are you?

Multiline String Literal

A multi-line string is a group of characters or a string that spans several lines. These strings are again implemented in two different ways:

  • With the addition of a backslash at the end of each line:


str="Good evening!\ How are you?\


Good evening! How are you?

  • Using triple quotes

We may easily generate a multi-line string in Python by using triple quotes at the beginning and end of the string.


t = """Good evening!

How are you? """

print (t)


Good evening! 

How are you?  

Numeric Literals In Python

Based on the nature and magnitude of the numbers, there are various forms of numeric literals: integer, large integer, complex numbers, and float (decimal numbers). Let's examine each of them individually.

Integer Literals

Integers make up all natural numbers, whether they are negative, zero, or positive. The following kinds can also be used to describe them:

  • Decimal integers: range from 0 to 9. The base of decimal numbers is 10.
  • Binary integers: only have the digits 0 and 1. Binary values start at base 2 and are prefixed with "0b."
  • Octal integers: composed of the digits 0 through 7. 8 serves as the octal basis. The octal values in Python are preceded by "0o."
  • Hexadecimal integers: consist of letters A through F and numerals 0 through 9.


a = 36

b = 52

print (a)

print (b)




Float Literals

Float literals are essentially real numbers made up of both integer and fractional components. Consider the following example to get a better idea of what are float literals:


c = 44.42

print (c)



Complex Literals

A complex — or imaginary — number, is essentially the negative number's root. Complex numbers have unique applications and significance and they can be specified in Python. Below, you can see a simple example of complex number literals in Python:


a = 6 + 9j

b = 4j

print (a)

print (b)




Boolean Literals In Python

Boolean literals are pretty straightforward in nature as they only represent a True or False value. Here’s a simple example of the Boolean literals code and output:


a = 4 > 2

print (a)

b = 48 < 49

print (b)




Literal Collections In Python

If you want to interact with several values in Python literal, you can use collections. Python has four different forms of literal collections:

List Literal

A list is a collection of objects of various data types. Lists are mutable, so they can be changed as necessary. To form a list, you should specify the values of various data types and enclose them in square brackets. By using the [:] slice operator, any value or group of values contained in a list can be retrieved. Lists are accompanied by two operators: concatenation (the + operator) and repetition (the * operator). Let's examine the examples provided below to learn how list literals function in Python:


l = [465, Anne Simons, 1+4.5j, 33.44]

print (l)

type (l)


[465, Anne Simons, 1+4.5j, 33.44]

Tuple Literals

Tuples are literals that can hold any data type and are declared by using round brackets. The components of Tuple literals are separated by commas. Unlike list literals, these are immutable.


even_numbers = (2, 4, 6, 8)

vowels = ('a','e','i','o','u')  

print (even_numbers)

print (vowels)


(2, 4, 6, 8)

('a', 'e', 'i', 'o', 'u')

Dictionary Literals

A dictionary is a set of data that uses a key-value format to store value. These are set out by commas and contained in curly braces. These literals can hold a variety of data types and are mutable. See the below example to understand the implementation of dictionary literals: 


my_dict = {'a': 'apple', 'b': 'bat', 'c': 'car'}

print (my_dict)


{'a': 'apple', 'b': 'bat', 'c': 'car'}

Set Literals

Set literals are a non-modifiable collection of unordered data. The components are separated by commas and enclosed in curly brackets. The below example outlines the use of set literals in Python:


vowels = {'a', 'e', 'i', 'o', 'u'}

print (vowels)


{'o', 'e', 'a', 'u', 'i'}

Special Literal in Python

None is a unique literal in the Python language. It denotes that a specific field hasn’t been created yet. When we print a variable in Python without a value given to it, Python will output None. Python also uses "none" at the end of lists. Check this example: 


my_list = ['Dog', 'Cat', None]

print (my_list[2])



Final Thoughts

Literal is one of the many features that Python programming offers to help you apply an idea in the simplest way possible. Literals enable you to determine the input value as necessary for data of any type. They can be quite helpful in programs that use variables with various data types. Hopefully, this post and the real-life examples helped you better understand literals in Python and how to use the various types. Whether you're just starting out or you're an experienced Python programmer, we have a tutor that can help you master your Python programming coursework. If you're stuck on a question or need help with Python homework our tutors will help you to solve it!


Live Chats