Hey, peep! This is a connected tutorial from the previous one where we saw the detail of numeric data types. This time, we are moving forward with the other data types in Python. We are understanding all these concepts with the help of examples and practising the little but understandable codes in TensorFlow. Different types of operations are also performed on these data types so that you may have an idea of why we are differentiating all these data types and how we can categorize all of them into different groups. Keep in mind that all of these concepts are for deep learning, and we want to make sure that we will not face any problems in the complex work of deep learning; therefore, we are moving slowly and steadily in Python. So, have a look at the content you are learning in this tutorial, and after that, we’ll start the introduction.
What are the strings in Python?
How do you declare the string in different ways while working in Python?
What are escape sequences in Python?
How can you use the triple quotation in Python and why it is useful?
Each concept will be discussed with the help of simple and easy codes and the description of each of them is discussed in detail in this lecture. This is a connected part of the lecture that was discussed in the last lecture and other data types will be mentioned in the next lecture.
Strings in Python
A string is nothing but the combination of different alphabets in a specific sequence. In programming, the concepts are a little bit different from those in the real world. The way we speak in English is said to be "string" in the programming language, and it is an important data type in any programming language for non-programmers, anything that comes on the screen must be easy to understand, and string is the best way to print the message on the screen. Let us define the string in simple words.
"The string is the sequence of characters or alphabets that specify the message on the screen and it is denoted by 'str' in Python."
We always say that python is simpler and easier than other programming languages, and it is true for the concept of string as well. In Python, the string can be denoted by using single or double quotation marks and the usage of commas is according to the choice of the programmer. In other words, you can use the single or double inverted commas around the alphabet to represent the string. Moreover, you must know that string has no limited length same as in the case of integers. The only thing that limits the length of the string is the memory space of the system you are using. Have a look at the syntax of the string while you are working on Python.
Python String in TensorFlow
First of all, you have to look at the syntax of the string. The syntax of all the data types is the same; therefore, we are mentioning just this one, and after that, you will have an idea of how to implement the other data types. We have mentioned in the previous lectures that you just need the name of the variable and then the value of the variable in the form of any data type you want, so the syntax is given as
string = "I am learning Python for Deep learning."
The name may be anything, but it is important to use inverted commas (either single or double). TensorFlow will make this clearer, but there is a short procedure to get TensorFlow up and running.
Search for the “Anaconda Navigator” on your PC.
In the environment section, click on the Jupyter lab and launch TensorFlow.
The new tab will open in your browser with the name “Local host.”
Go to the new cell and start coding there.
Now, you have to write the following code in the new cell and run the program to get the output.
string="I am learning at TheEngineeringProjects"
print(string)
a=" "
print(a)
b='Python is best for Deep learning'
print(b)
The output of this code is given as:
From the code and output, we can conclude with the following point:
The name of the variable may be anything.
We can use single or double inverted commas for the string and the result will be the same.
A string may be an empty space so we can say that length of the string is zero to positive infinity.
The output of each print function is always shown in the next line in normal conditions.
So, the best way to show any message to non-programmers is in the form of a string.
Escape Sequence in Python
In most high-level programming languages, there are certain words that are chosen to be used for a special task in Python with the help of their special sequence. These are known as the escape sequence. These are defined as:
"The escape sequence in Python is the combination of characters that, when used inside a string or character, does not show itself but does the specific task according to its functionality."
It is important to notice that these are important concepts in Python because it is very difficult and in some cases, impossible to do the same task without using the escape sequence. Now, have a look at some of these in the next section and you will get the detail of some important escape sequences.
New Line in Text Using TensorFlow
As you can see, in the previous code, we made a space between two lines with the help of an empty string. But, what if we want to print a new line in the message? For this, we use a special operator in the string message and it is used in places when the output is long or there is the need for more than one line to represent the message more clearly. The operator is a backslash with an “n” that can be used at any place in the text. Have a look at one example to do so.
print('The backslash with "n" is used to \nprint a new line')
The output of this single-line program is interesting.
It is important to notice that if you want to work with this operator, you have to use it properly. You can not use any additional space between the text and this new line operator; otherwise, you will get an error message from the compiler.
Tab Operator in Python
We all use the tab on the keyboard but what if you want a space between the text? You can do so by applying the space between the text while you are printing the message but it is not the professional way. To do this with convenience, just like some other programming languages, Python has a special operator. Same as we use the n in the new line operator, if you use the t with the backslash, you can print the space between the text that is equal to eight space bars.
print('The backslash with "t" is used to \tprint a tab space in the line')
Let’s see what we have in the output of this code.
Same as these operators, we also have some other commands that do the work similar to this and the syntax of all of these is the same. For the convenience of the reader, we have made a table that contains all the information about these operators. Have a look at the table and after that, we will run all of these at once in our code in TensorFlow.
Name |
Representation |
Description |
New line |
\n |
It creates a new line in the text even if you are using it in the middle of the line. |
Tab space |
\t |
It is used for the space tab between the text. |
Bullet |
\a |
For the bullets in the text, we use this operator. |
Delete space |
\b |
To delete the space in the text, we use this operator. In this way, the text obtained only removes the space from the place where this operator is being used and the other text remains the same. |
Ignore the line |
\r |
By using the working of this operator, the text before the operator is deleted, and you will get the text after this operator only in the output. |
Arrow |
\v |
If you want to show a small arrow in the text, you will use this operator. |
To test each of the commands discussed before, we are rushing towards the TensorFlow, where we are using all of these in a similar ways to show the difference between all of these. Keep in mind, the syntax of each of them is the same. These are not the proper functions but are the pre-defined commands that are used less commonly in Python. Here is the homework task for you. You have to look at the code we are giving below and without cheating will guess the output.
print('We use \ncups for tea')
print('The tab create \teight spaces in the text')
print('\aBullets can be made using this operator in Python')
print('If you want to delete the \bspace, you can use the operator in Python')
print('This part will be ignore \ronly this will be printed on the screen')
print('\vThis small arrow looks cute')
Once you have guessed the output, now check this in your compiler, and then match the output with the one that is given next in the image.
The last arrow can also be used as the bullet in the text you want to show on the screen. Another task for you is use the different text and practice the code and string with your own message. It is the simplest and interesting task that you must try on your TensorFlow.
Triple Quotation String in Python
Here is another way to indicate that you want to declare the string. This is a less common way to represent the string, but if you are studying the string, you must know this. You can use double and single inverted commas around the text at the same time, and this will not cause any errors. Let me tell you about the workings of this kind of string. After that, I’ll show you how you can use it for different purposes.
print('''Deep learning is the subclass of the artificial intelligance''')
So, we are using three quotation marks around our text and getting the same output as for the single and double inverted commas. Here is the output:
The advantage of using this method is, you do not need any new line operators to start the text on a new line, but you will write the code as you want the output.
This is a more interesting and convenient way to write your text. Right now, you must be wondering why we are highlighting such uses and creating this hype. Yet, you must know, the aforementioned ways of writing the codes will help you a lot when you will go to the complex and long codes for the deep learning. One of the application of this way to declare the string is in this lecture. We can use this way to declare the string and can perform all the escape sequence command by declaring a single string in different lines so that we may not have to write “print command” every time when we are working with a new escape sequence.
Hence, we have read a lot about strings today. It was an interesting lecture where we saw what strings are and how we can use them in our coding in different ways. We say the representation, working, and the escape sequence between the string. The syntax of each case was clarified with the help of examples in TensorFlow. You will get information about more data types in the next lecture.