What are literals in Python? When writing programs in Python, we need to make use of different kinds of values. When we represent values of a data type in code, we are making use of Literals.

Literals are the raw data representing values of a data type and can be assigned to variables. Python has literals for its elementary data types, and these literals can be used to construct values for these types.

In Python, Literals can be String literals, Numeric, Boolean, represent collection types, or be the Special Literal None. The following sections explore each of these literal types.

String Literals in Python

String literals are used to represent a string value. A string is a sequence of characters. Therefore, a string literal consists of a sequence of zero or more characters, enclosed with single quotes or enclosed with double-quotes.

# String literals in single quotes
val = 'this is a string literal'

# string literal in double quotes
val = "this is also a string literal"

In Python, we represent an empty string/null string with just single quotes, or double quotes, and no characters within the quotes.

# Empty string with single quotes
val = ''

# empty string using double quotes
val = ""

The previous examples represented a string in a single line. If the string is too long, we can break it into multiple lines and represent it as a multiline string. A multiline string is string broken into multiple lines and enclosed with triple single quotes, or triple double quotes.

val = '''This is an example
of a string having multiple
lines of text'''


# multiline text, using double quotes
val = """This is another example
of a string having multiple lines
of text, but making use
of double quotes"""



This is an example
of a string having multiple
lines of text 

This is another example
of a string having multiple lines
of text, but making use
of double quotes

Numeric literals

Python also provides a way to represent numeric values in code. The next sub-sections show how to represent integer, float, or even complex number values in Python code.

Integer Literals

If you need a way to represent whole numbers, you will need to make use of Integer Literals. An integer literal is simply a sequence of one or more digits.

# Integer literals in base ten
int_val = 23
int_val = -34
int_val = 0

Python also allows representing whole numbers in bases two (binary), eight (octal), and sixteen (hexadecimal). The following examples show how to represent numbers in these other bases.

# integer literals, base two/binary
bin_val = 0b111001011

# integer literals, base eight/octal
oct_val = 0o25712

# integer literals, base 16/hexadecimal
hex_val = 0xf2a

print(bin_val, oct_val, hex_val)

When we print out the variables, we get the numbers in base ten


459 11210 3882

Float literals in python

Python float literals allow us to represent fractional numbers. In python, a float literal is a sequence of digits; the whole number part and the fractional parts are separated with a single decimal point.

# integer literals, base two/binary
# Float literals
f_val1 = 5.98
f_val2 = 235.8
f_val3 = 0.00032

Complex numbers

A complex number is a sum of a real number and an imaginary number. Python also allows us to represent complex numbers directly in code with the a + jb notation, a and b are integers or floats.

# complex literals
cmplx_val1 = 3j
cmplx_val2 = 2 - 5j
cmplx_val3 = -4j + 5

Boolean literals in Python

Making decisions in code would not have been possible without Boolean types. A boolean value is binary, either 0 or 1, Yes or No, True or False. In python, we can represent boolean literals using either True or False.

bool_val1 = True
bool_val2 = False

Literals for collection data types

Lists, Dictionaries, Tuples, and Sets are the available collection types in Python. Python also provides a way to represent objects of these collection types in code directly.

# literals for collections

list_literal = [2, "hello", True, 2 - 6j]
tuple_literal = (2, "hello", True, 2 - 6j)
dict_literal = {'int': 2, 'str': "hello", 'bool': True, 'complex': 2 - 6j}
set_literal = {2, "hello", True, 2 - 6j}

print(list_literal, type(list_literal))
print(tuple_literal, type(tuple_literal))
print(dict_literal, type(dict_literal))
print(set_literal, type(set_literal))


[2, 'hello', True, (2-6j)] <class 'list'>
(2, 'hello', True, (2-6j)) <class 'tuple'>
{'int': 2, 'str': 'hello', 'bool': True, 'complex': (2-6j)} <class 'dict'>
{(2-6j), True, 2, 'hello'} <class 'set'>

The special Literal – None

Python allows us to use the value None for a null value.

# null
var = None

print(var, type(var))


None <class 'NoneType'>


This article discussed and demonstrated the use of literals in python. Please check out our other articles on python such as removing punctuation from strings in Python and keep improving your skills.