Python Literals

The raw values you write directly in code โ€” numeric, string, boolean, and the special None โ€” and how each one is actually represented under the hood.

Overview

A literal is raw data (a fixed value) directly given/assigned to a variable in Python. Python has four main categories of literals:

Key Concepts

1. Numeric Literals

Python supports four numeric literal types: binary, decimal, octal, and hexadecimal integers, plus float and complex literals.

a = 0b1010          # Binary Literal (prefix 0b)
b = 100             # Decimal Literal
c = 0o310           # Octal Literal (prefix 0o)
d = 0x12c           # Hexadecimal Literal (prefix 0x)

# Float Literal
float_1 = 10.5
float_2 = 1.5e2     # scientific notation -> 150.0
float_3 = 1.5e-3    # scientific notation -> 0.0015

# Complex Literal
x = 3.14j           # 'j' suffix denotes the imaginary part

print(a, b, c, d)
print(float_1, float_2, float_3)
print(x, x.imag, x.real)

Output:

10 100 200 300
10.5 150.0 0.0015
3.14j 3.14 0.0

How to read the output

VariableLiteral WrittenValue PrintedNotes
a0b101010Binary โ†’ Decimal conversion
b100100Plain decimal
c0o310200Octal โ†’ Decimal conversion
d0x12c300Hex โ†’ Decimal conversion
float_110.510.5Normal float
float_21.5e2150.01.5 ร— 10ยฒ
float_31.5e-30.00151.5 ร— 10โปยณ
x3.14j3.14jComplex number
x.imagโ€”3.14Imaginary part
x.realโ€”0.0Real part (0 since only imaginary was given)
Note: Even though the literals are written in binary/octal/hex, print() always displays the equivalent decimal value.

2. String Literals

Strings can be created with single quotes, double quotes, triple quotes (multiline), unicode escapes, or as raw strings.

string = 'This is Python'          # single quotes
strings = "This is Python"         # double quotes
char = "C"                         # single character (still a string in Python)
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\U0001f600\U0001F606\U0001F923"   # unicode escape sequences (emojis)
raw_str = r"raw \n string"         # raw string literal

print(string)
print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)

Output:

This is Python
This is Python
C
This is a multiline string with more than one line code.
๐Ÿ˜€๐Ÿ˜†๐Ÿคฃ
raw \n string

Types of String Literals

TypeSyntaxPurpose
Single-quoted'text'Basic string
Double-quoted"text"Basic string (same as single-quoted)
Character"C"Python has no separate char type โ€” a single character is just a 1-length string
Multiline"""text""" or '''text'''Allows string to span multiple lines
Unicodeu"\U0001f600"Represents Unicode code points (e.g., emojis)
Raw stringr"raw \n string"Escape sequences like \n are treated as literal characters, not interpreted (i.e., \n prints as \n, not a newline)
Tip: Raw strings (r"...") are very useful for regex patterns and file paths on Windows, where you don't want \n, \t etc. to be interpreted as escape sequences.

3. Boolean Literals

True and False are Python's Boolean literals. Internally, Python treats:

This means Booleans can be used directly in arithmetic operations.

a = True + 4      # 1 + 4
b = False + 10    # 0 + 10

print("a:", a)
print("b:", b)

Output:

a: 5
b: 10
Important Point: This works because bool is technically a subclass of int in Python.

4. Special Literal โ€” None

None is Python's special literal used to represent the absence of a value (similar to null in other languages).

a = None
print(a)

Output:

None

โš ๏ธ Common Beginner Mistake: NameError

If you try to use a variable that hasn't been assigned a value yet, Python raises a NameError.

k

Output:

NameError: name 'k' is not defined
  1. Running the cell with just k on its own โ€” without k = <something> โ€” caused Python to look up k as an already-existing name, which doesn't exist yet.
  2. Result: NameError: name 'k' is not defined.
โš ๏ธ Key takeaway: In Python, simply "declaring" a variable name (like you might in some other languages) does nothing. A variable only comes into existence once you assign it a value, e.g. k = None or k = 5.

Things to Remember

Quick Revision

Python literals = the four kinds of raw values you can write directly in code:

  1. Numeric โ†’ binary (0b), decimal, octal (0o), hex (0x), float (incl. e notation), complex (j suffix).
  2. String โ†’ single/double quotes, multiline ("""), unicode escapes (u"\U..."), raw strings (r"..." โ€” ignores \n etc.).
  3. Boolean โ†’ True/False, which act as 1/0 numerically.
  4. Special โ†’ None, meaning "no value assigned."
โš ๏ธ Always assign a value before using a variable, or you'll hit NameError: name 'x' is not defined.