Python

Python string literals

In Python, the literals are defined as data specified by variables. Literals are usually the notation used to show fixed values that are present in the source code.

If we want to write multiple lines in the string, we enclose the string with triple quotes. String literals define the indications of the strings. We use backslash right after the single quotation mark if we want the actual element of the string literal, which is enclosed by single quotes. This shows the python not to end the string. The elements that are introduced by a backslash have some specific meaning. Byte literals create an example of the byte’s type. Only ASCII characters can be included. Bytes with numbers greater than or equal to 128 must be escaped. The string literals and byte literals can either be merged with the character `r` or` R`. These strings are known as raw strings. The formatted string is combined with ‘r’ to make raw formatted literal. But the bytes’ literals cannot be combined with ‘r’.

In this article, we talked about different categories of literals that are used in Python.

String literals:

In string literals, the elements or text are enclosed in different ways. It includes the single(‘), double(“) and triple(”’) quotation marks. We can write multiple strings by the use of triple(”’) quotes.

For carrying out the Python codes, we installed spyder5. We have to create a new project for code by tapping the “Ctrl + N” from the keyboard. The name of a new file is “untitled.py2”.

In this example, we create different variables for the string. ‘X’ variable is given the string Modern programming language. ‘Y’ variable is assigned to the same string again. But here, we use double quotes(“) to surround the string. The third time the same string literal is assigned to variable ‘z’. Now for running the code, we pressed the “F5” button.

The print statement prints the string “Modern programming language” after the string is enclosed in three different ways. In last, the string surrounded by triple quotation marks(”’) gives an output in our desired way.

Character literal:

It is that type of string literal in which single or double quotation marks enclose every character.

In this instance, we make two variables named ‘x’ and ‘y’. The character L is assigned to variable ‘x’. Here L is enclosed by a single quote. The character E is assigned to variable ‘y’. But here, E is surrounded by double quotes.

Now, after running the code, we get the characters L and E as an output.

Numeric literals:

The numeric literals are unchangeable in Python. They cannot be modified. There are three categories of numeric literal, including integer, float, and complex literal. Integer literal contains both positive and negative numeric numbers. It has no fractional part. The float literal contains both positive/negative numbers and fractional numeric numbers. Complex literal consists of one real part and another complex part.

In this instance, we create four different variables. We allot integer literals to these variables. ‘0b10100’ is assigned to variable ‘l’. ‘300’ is assigned to variable ‘m’. 0o320′ is assigned to variable ‘n’. ‘0x12b’ is assigned to the last variable ‘o’.

Now we make the other two variables (float_1, float_2). We assigned different floating numbers to these variables. ‘489.5’ is given to ‘float_1’. ‘23.0’ is allowed to ‘float_2’. We create a ‘b’ variable for the complex literal. The complex literal is ‘8+6.47j’.

We call print statements to these literals. For the integers, ‘l’ is a binary literal and returns ’20’ value, ‘m’ is a decimal literal and returns ‘300’ value, ‘n’ is an octal literal and returns ‘300’ value, ‘o’ is a hexadecimal literal and returns ‘301’ value. After applying the print statements, these literals are changed into decimals.

The floating-point literals return ‘489.5’ and ‘23.0’. As these are floating-point numbers. In the complex literal, ‘8’ is a real part and ‘6.47’ is an imaginary part. It contains both integer and fraction.

Boolean literals:

Python has two Boolean literals. They include True or False. True shows the ‘1’ value, and False shows the ‘0’ value.

In this example, we take four different strings named ‘l’, ‘m’, ‘n’, ‘o’. After assigning Boolean literals to the strings, we assigned the Boolean literal false to the string ‘l’, and true to the string ‘m’. We operate by adding the next two strings ‘n’ and ‘o’.

We add the value of false literal into 9 to get the value of string ‘n’. We also add 5 into the true literal to find out the value of string ‘o’. Now we call the print statement of these four strings.

The print statement returns false to the value of string ‘l’ as we assigned a false literal to this string. Returns true to string ‘m’ as we assigned true literal to this string. ‘l’ is false, and ‘m’ is true as 1 is equal to true. Returns 9 to the string ‘n’ after the addition of false literal. And at last, we get 6 after the addition of true literal.

List literals:

The list consists of elements of various data types. The elements which are saved in the list are isolated by comma(,). The elements are surrounded by square brackets[]. We save multiple types of data. Lists are changeable so that we can modify the lists.

In this code, we create two lists. One list contains the first seven even numbers. The second list contains different names of boys and girls; also, it has the element ‘8’. These lists are changeable. We want to print the lists of even numbers and a list of names.

The print statement prints both lists. We get the list of the first seven even numbers and a list of different names.

Conclusion:

As we see that Python has various types of literals. String literal in which the elements are enclosed by single, double, and triple quotation marks. Character literal in which character is surrounded by quotation marks. Numeric literals in which quotation marks surround different numeric values including integer, floating, and complex. I hope you will get clear guidance while reading this tutorial.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content