Literals (often referred to as constant- Values) are data items that have a fixed value. Python allows several kinds of literals :
- Strings literals
- Numeric literals
- Boolean literals
- Special Literals None
- Literals Collections
The text enclosed in quotes forms a string literals in Python. For example, ‘a’, ‘abc’,”abc” are all string literals in Python.l Unlike many other languages, both single character enclosed in quotes such as “a” or ‘x’ or multiple characters enclosed in quotes such as “abc” or ‘xyz’ are treated as String literals.
Note- A string literals is a sequence of characters surrounded by quotes (single or double or triple quotes).
As you can notice, one can form string literals by enclosed text in both forms of quotes _ single quotes or double quotes. Following are some valid string literals in Python :
‘Astha’ “Rizwan” ‘Hello world’ “Amy’s” “129045” ‘1-x-0-w-25’ “112FBD291”
Note- In python, one can form string literals by enclosing text in both forms of quotes -single quotes or double quotes.
Python allows you to have certain nongraphic-characters in strings values. Nongraphic characters are those characters that cannot be typed directly from keyboard e.g., backspace, tabs, carriage return etc. (Non character is typed when these keys are pressed, only some action takes place).These nongraphic-characters can be represen-ted by using escape sequences. An escape sequence is represented by a backlash(\) followed by one or more characters.¹
In the above tables, you see sequences representing \, ‘,”. Though these characters can be typed from the keyboard but when used without escape sequence, these carry a special meaning and have a special purpose, however, if these are to be typed as it is, then escape sequences should be used. (In Python, you can also directly type a double-quotes inside a single-quoted string and vice-versa. e.g., “anu’s” a valid string in Python)
String Types in Python
Python allows you to have two string types :
- Single-line Strings
- Multiline Strings
Single-line Strings ( Basic strings). The strings that you create by enclosing text in single quotes (‘ ‘)or double quotes (” ” ) are normally single-line strings, i.e., they must terminate in one line. To understand this, try typing the following in IDLE window and see yourselves :
Text = ‘hello there’
Python will show you can an error the moment you press Enter key after hello (see below)
Note- An escape sequence represents a single character and hence consumes one byte in ASCII representation.
The reason for the above error is quite clear- Python by default creates single-line strings with both single or double quotes. So, if at the end of a line, there is no closing quotation mark for an opened quotation mark, Python shows an error.
2. Multiline Strings. Sometimes you need to store some text spread across multiple lines as one single string. For that Python offers multilione strings.
multiline strings can be created in two ways :
(a) By adding a backslash at the end of normal single-quote / double-quote strings. In normal strings, just add a backslash in the end before pressing Enter to continue typing text on the next line. For instance,
Text1 = ‘hello\ do not indent when continuing world typing in text line after ‘\’
Note- A basic string must be completed on a single line, or continued with a backslash (\) as the very last character of a line if it is to be closed with a closing quote in next line.
Following figure shows this :
(b) By typing the text in triple quotation marks. (No backslash needed at the end of line). Python allows to type multiline text string by enclosing them in triple quotation marks (both triple apostrophe or triple quotation marks will work).
Note- A multiline string continues on until the concluding triple-quote or triple-apostrophe.
Size of Strings
Python determines the size of a string as the count of characters in the string. for example, size of string “abc” is 3 and of ‘hello’ is 5. But if your string literal has an escape sequence contained within it, then make sure to count the escape sequence as one character. Consider some examples given below :
For multiline strings created with triple quotes, while calculating size, the EOL (end-of-line) character at the end of the line is also counted in the size. For example, if you have created a string Str3 as :
then size of the string Str3 is 5 (three characters a,b,c and two EOL characters that follow characters a and b respectively).
For multiline strings created with single/double quotes and backslash characters at end of the line, while calculating size, the backslashes are not counted in the size of the string ; also you cannot put EOLs using return key in single/double quoted multiline strings e.g.,
Str4 = ‘a\ b\ c’
The size of string Str4 is 3 (only 3 characters, nop backslash counted.)
To check the size of a string, you may also type len(<stringname>) command on the Python prompt in console window shell as shown in the following figure :
Note- Use len(<object name>) to get the size or length of an object.
The numeric literals in Python can belong to any of the following three different numerical types :
often called just integers or ints, are positive or negative whole numbers with no decimal point.
Floats represent real numbers and are written with a decimal point dividing the integer and fractional parts.
are of the form a + bJ, where a and b are floats and J (or j) represents √-1, which is an imaginary number). a is the real part of the number, and b is the imaginary part.
Let us talk about these literal types one by one.
Integer literals are whole numbers without any fractional part. The method of writing integer constants has been specified in the following rule :
An integer constant must have at least one digit and must not contain any decimal point. It may contain either (+) or (-) sign. A number with no sign is assumed to be positive. commas cannot appear in an integer constant.
Python allows three types of integer literals :
- Decimal Integer Literals. An integer literal consisting of a sequence of digits is taken to be decimal integer literals unless it begins with 0 (digit zero). For instance, 1234, 41, +97, -17 are decimal integer literals.
- Octal Integer Literals. A sequenceof digits starting with 0o (digit zero followed by letter o) is taken to be an octal integer. For instance, decimal integer 8 will be written as 0o10 as octal integer.
- Hexadecimal Integer Literals. A sequence of digits preceded by 0x or 0X is taken to be ab hexadecimal integer.
Thus, number 12 will be written either as 12 (as decimal), 0o14 (as octal) and 0XC (as hexadecimal).
A hexadecimal value can contain digits 0-9 and letters A-F only i.e., 0XBK9, ox19AZ etc., are examples of invalid hexadecimal numbers as they contain invalid letters, i.e., letters other than A-F.
NOTE- Many programming languages such as C, C++, and even Python 2.x too have two types for integers : int (for small integers) and long (for big integers). But in Python 3.x, there is only one integer type <class ‘int’> that works like long integers and can support all small and big integers.
Floating Point Literals
Floating literals are also called real literals. Real literals are numbers having fractional parts. These may be written in one of the two forms called Fractional Form or the Exponent Form.
Fractional Form. A real literals in Fractional Form consists of signed or unsigned digits including a decimal point between digits.
The rule for writing a real literal in fractional form is :
A real constant in fractional form must have at least one digit with the decimal point, either before or after. It may also have either + or – sign preceding it. A real constant with no sign is assumed to be positive.
The following are valid real literals in fractional form :
2.0, 17.5, -13.0, -0.00625, .3 (will represent 0.3), 7. (will represent 7.0)
The following are invalid real literals:
2. Exponent form. A real literal in Exponent form consists of two parts : mantissa and exponent. FOr instrance, 5.8 can be written as 0.58×10¹ = 0.58 E01, Where mantissa part is 0.58 (the part appearing before E) and exponent part is 1 (the part appearing after E). E01 represents 10¹ .
A real constant in exponent form has two parts : a mantissa and an exponent. The mantissa must be either an integer or a proper real constant. The mantissa is followed by a letter E or e and the exponent. The exponent must be an integer.
The following are the valid real literals in exponent form : 152E05, 1.52E07, 0.152E08, 152.0E08, 152E+8, 1520E04, -0.172E-3, 172.E3, 25E-4, 3.E3 (equivalent to 3.0E3)
(Even if there is no preceding or following digit of a decimal point, Python 3.x will consider it right)
The following are invalid real literals in exponent form :
Numeric values with commas are not considered int or float value, rather Python treats them as a tuple. A tuples is a special type in Python that stores a sequence of values. (You will learn about tuples in coming chapters – for now just understand a tuple as a sequence of values only. )
The last invalid example value given above (17,225e02) asks for a special mention here.
Any numeric value with a comma in its mantissa will not be considered a legal floating point number, BUT if you assign this value, Python won’t give you an error. The reason being is that Python will not consider that as a floating point value rather a tuple.
Carefully have a look at the adjacent figure that illustrates it.
We are not talking about Complex numbers here. These would be discussed later when the need arises.
A Boolean literals in Python is used to represent one of the two Boolean values i.e., True (Boolean true) or False (Boolean false). A Boolean literals can either have value as true or as false.
NOTE- True or false are the only two Boolean literals values in Python None literals represents absence of a value.
Special Literals None
Python has one special literals, which is None. The None literal is used to indicate absence of value. It is also used to indicate the end of lists in Python.
The None value in Python means “There is no useful information” or “There’s nothing here”. Python doesn’t display anything when asked to display the value of a variable containing value as None. Printing with print statement, on the other hand, shows that the variable contains None (see figure here)
Python supports literal collections also such as tuples and lists etc. But covering these here would make the discussion too complex for the beginning. So, we’ll take them at a later time.
NOTE- Boolean literals True, false and special literal None are some built-in constants/literals of Python.