Skip to main content

Python Literals

Literals

A literal is a raw data given in a variable or constant.

There are several types of literals in Python that are described below:

Numeric Literals

Numerical Literals are immutable (i.e., not changeable).

Numeric literals can belong to 3 different numeric types: Integer, Float and Complex.

Numeric Literals

# Numeric Literals
a = 0b1010 #Binary Literals
b = 100 #Decimal Literal
c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal
print(a, b, c, d)

Output

10 100 200 300

Float Literals

#Float Literal
float_1 = 12.3
float_2 = 1.5e2
print(float_1, float_2)

Output

12.3 150.0
note

When variables are printed, all literals are converted to decimal values.

Complex Literals

#Complex Literal 
x = 1.23j
print(x, x.imag, x.real)

Output

1.23j 1.23 0.0
note

To learn more about Numeric Literals, see Python Numbers.

String literals

A string literal is a sequence of characters surrounded by quotation marks.

Both single and double or triple quotes can be used for a string. A character literal is a single character surrounded by single or double quotes.

strings = "This is a string"
char = "C"
multiline_str = """This is a multiline string."""
unicode_str = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"

print(strings)
print(char)
print(multiline_str)
print(unicode_str)
print(raw_str)

Output

This is a string
C
This is a multiline string.
Ünicöde
raw \n string

where:

  • This is a string is a string literal.
  • C is a character literal.
  • The value in triple quotation mark """ assigned to multiline_str is a multi-line string literal.
  • The string u"\u00dcnic\u00f6de" is a Unicode literal which supports characters other than English. In this case, \u00dc represents Ü and \u00f6 represents ö.
  • r"raw \n string" is a raw string literal.
note

To learn more about String Literals, see Python String.

Boolean literals

A boolean literal can have one of two values: True or False.

note

In Python, True represents the value 1 and False represents the value 0.

x = (1 == True)
y = (1 == False)

print("x is", x) # print x is True
print("y is", y) # print y is False

where:

  • x is True because 1 is equal to True
  • y is False because 0 is equal to False

Similarly, True and False can be used in numeric expressions as values: True is 1 and False is 0.

a = True + 10
b = False + 100

print("a:", a) # print 11
print("b:", b) # print 100

where:

  • a is 11 because True has a value of 1
  • b is 100 because False has a value of 0
note

To learn more about Boolean Literals, see Python Boolean.

Special literals

None is a special Python literal used to specify that a field has not been created.

More precisely, None is a particular data type that indicates a null value, the absence of data. None does not equal False, is not 0, and is not an empty string.

For example

print(None == '')      # print False
print('' == None) # print False
print(0 == None) # print False
print(False == None) # print False
print(None is None) # print True
note

Every variable whose value is None points to the same, and unique, None object. Therefore, to compare an object with None you must use is, not ==.

In a Boolean evaluation, None is considered False.

print(bool(None))  # print False
print(not None) # print True

Literal Collections

There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.

For example:

#list
colors = ["red", "blue", "yellow"]

#tuple
numbers = (1, 2, 3)

#dictionary
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'}

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

print(colors)
print(numbers)
print(alphabets)
print(vowels)

Output

["red", "blue", "yellow"]
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}
note

To learn more about literal collections, see Python Datatypes, List, Tuple, Dictionary and Set