Updated June 12, 2023
What are Python Literals?
In Python, literals refer to the data specified in a variable or constant. So, literal corresponds to a short and easy way to specify a value. There are various types of literals in Python, such as string literal, integer literal, long integer literal, float, etc. When a literal is evaluated, an object of the given type is yielded. The value can be approximated as needed. Literals, in almost all cases, correspond to immutable data types. As a result of this, an object’s identity becomes less important when compared to its value.
Types of Python Literals
In Python, there are various types of literals. We shall see each of them as discussed in the below section.
1. String Literal
When a text is enclosed in quotes, a string literal is formed. Based on the need, single or double quotes can specify a string literal. There are multiple ways to specify a string literal that forms various types. These are:
Single Line String Literal
When strings are specified in a single line only then, that becomes a single line literal.
Code:
t = 'Good Morning!'
print (t)
Output:
Multiline String Literal
When we intend to use multiple lines for specifying the string, that becomes a multiline string literal. To accomplish this, backslash ‘\’ is used.
Code:
t = "Hi!\
How are you?"
print (t)
Output:
As seen above, the backslash was specified after “Hi,” and then we moved to the next line. This functionality is quite helpful in situations that involve establishing big strings that we don’t prefer to be specified in a single line.
There’s another method to specify the string in multiple lines. This is through the use of triple quotes. For instance, we want to say, “Hi! How are you? It’s a long since we last met”. We would like to have this string specified using the triple quotes method. It will be done in Python, as shown below.
Code:
t = """Hi
How are you?
It's long since we met last."""
print (t)
Output:
As can be seen in the above Python code execution screenshot, the output gets appropriately indented. So this approach allows us to format the output as needed.
2. Numeric Literals
Numeric literals are of multiple types based on the number type and size. The types of numeric literals are integers, long integers, float (decimal numbers), and complex numbers. Let’s see each of them one by one.
Integer Literals
They can be either positive or negative. In Python, integers can be assigned to a variable, as illustrated below.
Code:
a = 23
b = 45
print (a)
print (b)
Output:
Float Literals
These are real numbers that consist of both integers and fractional parts. Most of the time, they are the result of a division operation. The following example illustrates the use of Python Float Literals.
Code:
c = 34.62
print (c)
Output:
As we can see in the above screenshot, C has been assigned a float value. While printing as output, C cannot be printed directly but first needs to be converted to a string as C is a float variable. Therefore, we used the str() function in the print statement.
Complex Number Literals
A complex number is an imaginary number, basically the root of a negative number. Complex numbers have their significance and applications. Python allows us to specify complex numbers like any other variable. The following example illustrates the specification and use of complex literals in Python.
Code:
i = 1.5J
a = 1 + i
b = a + 3j
print(a)
print(b)
type(a)
type(b)
Output:
In the above code, we specified a complex number. Python does not take 1.5j as a string but as a complex number. Then we performed additional operations. And finally, we printed the complex numbers as well as their types. Let’s see how the above code works in Python.
As demonstrated above, Python considers any number of the form nj as a complex literal. The output proves the type() function in the above program code.
3. Boolean Literals
These are the literals that can have either True or False value. Let’s review the following program code to see how Boolean literals work in Python.
Code:
a = 5 > 3
print(a)
b = 55 < 33
print(b)
Output:
As we can see above, the first condition is true, so variable a gets a True value. In the second case, the condition evaluates as False, so the system assigns False to b.
4. List Literal
A list in Python is a set of items of different data types. Lists can be modified as needed and are even mutable. To create a list, specify the values of different data types separated by a comma and enclose them in square brackets. Any value or multiple values stored in a list can be retrieved using a slice operator, i.e., [:]. Lists associate with two operators. These are concatenation, i.e., + operator, and repetition, i.e., ‘*’ operator. Let’s see how to list literals work in Python by reviewing the examples illustrated below.
Code:
l = [231, 'Mary Matthews', 1+3.5j, 43.56]
print(l)
type(l)
In the above code, we created a list containing an integer, string, complex number, and floating-point number. Next, we print the list and finally check the type of the variable l. Let’s see how the code performs in Python. This is shown in the following screenshot.
Output:
As we can see in the above screenshot, the type of variable l is the list. So, Python automatically recognizes a list based on the type of items present in the set.
Now, we create another list and add the two lists.
Code:
l1 = ['England', 122]
l2 = ['India', 123]
print(l1 + l2)
Output:
Conclusion
Amongst the various functionalities provided by Python programming to apply a concept most easily, literal is one. May the data of any type, literals allow the user to specify the input value as needed. Literals are pretty helpful in programs involving variables of different data types.
Recommended Articles
This is a guide to Python Literals. Here we discuss the Introduction and types of Python literals, which include string, numeric, boolean, and list literal. You may also look at the following articles to learn more –