What are numeric and string literals in Python?

Introduction

Literals are in every programming language and it is very important that you know what is meant by literals. Concepts seem simple, but often beginners get confused. It is very important that you should be familiar with these terms. Without the concepts of literals, it will be very hard to understand any programming language. This is because code can’t do away without literals and all programmers will frequently use this word and the concepts.

Basically, literals are notations for constant values of some built-in types. In Python there are two types of literals:

  • String literals
  • Numeric literals
String literals

String literals can be enclosed in matching single quotes (‘) or double quotes (“). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings).

Example

website = "www.bhutanpythoncoders.com"
country = 'Bhutan'
animals = """
          Cat
          Dog
          Cow
          """

All the strings(“www.bhutanpythoncoders.com”, ‘Bhutan’, and ” ” “
Cat
Dog
Cow
” ” ” ) assigned as a value to the variables in the above example is a string literal.

Numeric Literals

Numeric literals are the literals that deal with numerical constants. In Python, there are four types of numeric literals;

  1. plain integers
  2. long integers
  3. floating-point numbers
  4. imaginary numbers

Note: You might be wondering why there are no complex literals in the list despite it being also there in the Python language. It is because complex numbers are made of real numbers and imaginary numbers and it does not include a sign.

plain integers

Small integer constants with certain limits are called plain integer literals.

Example

age = 25
num1 = 13874

Long integer literals

There is no limit for long integer literals apart from what can store in available memory. The variable that stores big numbers is long integer literals.

Example:

num = 79228162514264337593543950336
Floating point numbers

Numbers that are represented in the decimal format are called floating-point number literals. Moreover, we can also represent exponents with the letter ‘e’ or ‘E’.

Example:

a = 3.14
b = 1e100

Imaginary numbers

An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are the numbers that are prefixed with the letter ‘j’.

Example:

c = 3j
d = 3 + 4j
img = 4.2 + 2.4j
img1 = 0.1j