Literals in Python
In this article, you will learn about the different types of literals in python.
A Literal in python is a raw data or a constant value that can get assigned to a variable. For example,
x = 100
Here 100 is literal, and we assigned it to a variable x.
There are different types of literals in python. For example,
- Numeric Literals
- String Literals
- Bool Literals
Numeric literals represent the numbers in python, and it can be of the following types i.e.
- Integer Literal:
- Positive & negative round numbers.
- For example, 100, -20, 15 etc.
- Float Literal:
- Positive & negative decimal numbers.
- For example, 10.89, -20.1, 15.22 etc.
- Binary Literal:
- The binary representation of numbers.
- For example, 00000110 is binary literal for 6
- Octal Literal:
- Octal representation of numbers.
- For example, o0027 is octal literal for 23
- Hexadecimal Literal:
- Hexadecimal representation of numbers.
- For example, ox0055 is hexadecimal literal for 85.
- Complex Literal: Represents the complex numbers.
- For example 5 + 7i
There are only two Boolean literals in python, i.e., True and False.
A String Literal in python is a group of characters. For example,
x = "Sample String" y = 'Another String' print(x) print(y)
Sample String Another String
We can enclose the string literals in either single quotes or double quotes, or triple quotes. Here x & y are two different variables and they are referring to two other string literals.
Multi-line String Literals
While defining a string literal, there is no difference between single or double-quotes. We can create a string literal by either or them. But if our string literal is a little big and consists of multiple lines, then we need to use the triple quotes (‘’’) to enclose a multi-line string literal. For example,
x = """This is a little big string""" print(x)
This is a little big string
or we can use three single quotes too,
y = '''This is a little big string''' print(y)
This is a little big string
We can also use the single or double quotes to define multi-line string literals, but we need to end the lines with an escape character ‘\’. For example,
z = "This is a big \ string, seriously \ very big string." print(z)
This is a big string, seriously very big string.
Although we provided the string in multiple lines, but there are no newline characters in the string because we used the ‘\’ to break the line.
Escape characters in string literals
We can escape characters in a string literal. Escape character starts with a ‘\’, and each escape character serves a special purpose.
For example, if we want to have a quote inside a string literal like — Varun’s car –, then we need to tell the interpreter that the given quote does not represent the end of the string. Instead, it is a part of the string literal. We can do that using an escape character \’. For example,
x = 'Varun\'s car' print(x)
Here \’ is an escape character and represents a single quote in the string. Some of the other escape characters are,
- \ : Newline continuation
- \\ : Display a single \
- \’ : Display a single quote
- \” : Display a double quote
- \b : Backspace
- \n : New Line
- \t : Horizontal Tab
- \v : Vertical Tab
- \r : Enter
There are three different types of literals in python. They are mainly used to initialize variables with hard coded values and sometimes also used in conditions.