Literals are a type of notation used in source code to represent a fixed value. They can also be defined as raw values or data that are given in variables or constants.
This article will teach you about the various types of literals in Python.
In Python, there are various types of literals. As an example,
In Python, numerical literals represent numbers and can be of the following types:
- Integer Literal:
- Both positive and negative numbers, including 0 are allowed. There must be no fractional parts.
- Ex : 100 , -200 , 300 etc.
- Float Literal:
- These are real numbers with integer and fractional components.
- Ex : 45.5 , 28.4 etc.
- Binary Literal:
- Numbers are represented in binary form.
- For example, 1010 is binary literal for 10
- Octal Literal:
- Numbers are represented in octal form.
- Hexadecimal Literal:
- Numbers are represented in Hexadecimal form.
- Complex Literal:
- Complex numbers are represented by this type.
- For example 6+ 3i ,4+2i etc.
In Python, only True and False are Boolean literals.
A string literal is made by enclosing a text(a set of characters) in single(”), double(“”), or triple(“”) quotes. We can write multi-line strings or show in the desired way by using triple quotes.
# string literals in python # single quote representation of string string1 = 'BTechGeeks' # double quotes representation of string string2 = "BTechGeeks" # multiline representation of string string3 = '''BTech Geeks Platform''' print('single quote string:', string1) print('double quote string:', string2) print('multiline string :', string3)
single quote string: BTechGeeks double quote string: BTechGeeks multiline string : BTech Geeks Platform
There is one special literal in Python (None). A null variable is described by the word ‘none.’ When ‘None’ is contrasted to something other than another ‘None,’ it returns false.
# special literal none special = None print(special)
- \ : Newline continuation
- \\ : Display a single \
- \’ : Display a single quote
- \” : Display a double quote
- \b : Backspace
- \n : New Line
- \t : Horizontal Tab
- \v : Vertical Tab
- \r : Enter