Python Literals

Literals are the notation for representing the fixed value in code. When declaring the variables, we pass the literal to the variable. we can also initialize the variable with a literal. Let’s see an example of Python Literal.

a=15
b='xyz'

The values 15 and ‘xyz’ are the literals. The literal is nothing but the value, we assign to a variable. When we say, the literal represents the fixed value, don’t confuse constant with literals. Literal is the value that the constant takes.

Python Literals:

We have the following literals in Python.

String Literal:

We can create a string literal by enclosing a sequence of characters with quotes. We can use single, double, or triple single or double quotes for a string literal. A character literal is a single character enclosed by single or double-quotes.

Single line string:

A single-line string is nothing but a string that can be accommodated in a single line.

st='xyz'

Multiline string:

A multi-line string is a string that is split across multiple lines. In Python, we can create a multi-line string in two ways:

1. A backslash at the end of each line:

In Python, the backslash acts as a line continuation character.

st='Example of \
multiline \
string'

print(st)
Example of multiline string

For more information on string literals, refer to Python Strings.

2. Using triple quotes:

We can also use triple quotes to enclose a multi-line string.

st='''Example of 
multiline
string'''
print(st)
Example of
multiline
string

Numeric literal:

Numeric literals are immutable. Python supports three numerical types:

Integer literal:

Integer literals includes both positive and negative numbers with no fractional or decimal part.

a=0b1100100 #binary literal
b=200 #decimal literal
c=0o454 #octal literal
d=0x190 #hexadecimal literal

print(a,b,c,d)
100 200 300 400

When we print the binary, decimal, and hexadecimal literals, all are converted to decimal literals.

Floating-point literal:

Floating-point literals are the real numbers with both integer and decimal parts. The output of addition and subtraction of integer and float will be floating-point numbers.

a=1.55
b=1.57e1

print(a,b)
1.55 15.7

The floating-point literal 1.57e1 is expressed with exponential. It is equivalent to 1.57*10.

Complex literal:

Complex numbers are represented in the format x+yj where x is the real part and y is the imaginary part.

a=2+11j

print(a,a.real,a.imag)
(2+11j) 2.0 11.0

Boolean Literal:

Boolean literal is one of the two values:

  • True
  • False
print(1==True)
print(1==False)
print(0==True)
print(0==False)
print(1+True)
print(1+False)
True
False
False
True
2
1

In general 1 represents True and 0 represents False. That is why 1+True results 2.

Special Literal:

Python has only one special literal: None

None specifies the field, that is not created.

def func():
    pass

result=func()
print(result)
None

Since the function did not return anything, the return value is None.

Literal collections:

Python has 4 different literals:

  • List literal
  • Tuple literal
  • Dictionary Literal
  • Set Literal
a=[1,2,3] #List literal
b=(1,2,3) #Tuple literal
c={'a':1,'b':2,'c':3} #Dictionary literal
d={1,2,3} #Set literal

print(a)
print(b)
print(c)
print(d)
[1, 2, 3]
(1, 2, 3)
{'a': 1, 'b': 2, 'c': 3}
{1, 2, 3}

Refer to lists, tuples, dictionary, and set tutorial to learn more about literal collections.

Translate ยป