Practical Python for Beginners Part #1.1::Strings

Ali Taha Dinçer
The Startup
Published in
6 min readOct 8, 2020

Welcome back! Last time, I introduced you to variables but the whole subtopics in topic needs more explanation for it’s own. So let’s start!

String is the data type that where we store the text data. You can use quotes or double quotes to indicate your data is a String. String data type is stored in the Python as “str” and you check the type by basically writing a line like:

Output of the interpreter for type() function

In industry, we use Integrated Development Environments (shorthand for IDE). These software that we use are help us to write codes. My favorite code editor (or IDE) is VSCode and I highly recommend you to use IDE’s to write your codes. I wrote this paragraph to show you this:

Code sugesstion features in VSCode

In VSCode, you can get code suggestions while you are writing your code. This is essential while learning a programming language because this suggestions are shown to you to get an idea while you are writing. The suggestions that you see from image above are basically what we call “methods”. Each object has it’s own methods or attributes in Python but this is another topic which we discuss in future. All you need to know is, there are things you can do with these methods and the detailed explanations are given in the box. I can explain them one by one in here, but rather than doing this, I encourage you to look them yourself, try them and see the outcomes. All I can give you is how to read the box on the right to understand what these methods do.

So in this example, lets start with the image above. You can see that we are currently looking into startswith() method and it’s explanation is given in the box. The information given in the above of the yellow divider is the initials/parameters and their types along with the returning value of the method. I know it is confusing but bear with me, it is easier than you thought. For this time, until we dive deep into OOP, stick with the explanation given in the below of the yellow divider. These sentences are enough for you to understand what the according method do.

To see the results of the method, you can print the functions. We have already used print function before but I have never talked about it. Print function (as the name suggest) prints the value inside of it to the screen. By screen, I mean the terminal. But wait, what is terminal. For now, you can basically think the terminal is the blackbox area under the IDE. For example:

Eample code
You have to press that green “run” button on the top right of the IDE to run your code
You can see the output below on the screen at the terminal

The important part while you are doing this is to adding parantheses in the end of the methods. Some of the IDE’s do it for you but in VSCode, you have to do it yourself or you can configure the settings on your IDE to achieve this. If you do not add the paranthesis at the end, than you will get unexpected results.

One of the important thing I need to say to you is, strings in Python can be considered as lists. In advance, you can act a string as a list. You will understand more about the topic that I give you below in the “Lists” article. But know, let’s give you some examples.

First of all, you can dive into any string by using brackets. For example:

The output of the code is:

As you can see, I used square brackets to get into specific parts of the string. This is called indexing and the operation that I did with square brackets are called indicing. But wait a minute! Why did I used 0? This is the most important topic in the computer science. From born, you have used 1 as the starting point but in computer science, we start from 0. So if I want to do something with the first character in my string, then I access it by using [0], to the second character I use [1] and so on. But what about that -1? In Python, you can start from the end by using negative index numbers. Since -0 do not exists, than you get the last character in a string by using [-1], for the second character at the end of string you can use [-2] and so on.

In addition to that, it is possible to get a portion of the string by using brackets and indicing. Such as:

And the output will be:

To explain more, line #2 prints “A” because it is the first character and #3 prints “.” as it is the last character. Line #4 prints the string portion from the 4'th character to 8'th. As you can see, line #5 did not print anything as there is a bug on the line. To see the exact output, you can change the -5 with -9 and -9 with -5. To understand why this is happening, just imagine the numerical axis. And finally, last line prints the proportion from the 4'th character in the string to the 5'th character in the string from the end.

Did these numbers confused you? Let me explain deeper. Indicing operation has a convention itself. The convention starts from the (index you choose+1) and ends in the (index you choose). Why line #4 printed the 4'th character? Because indexing starts from 0 and the index I chose was 3. Hence 3+1=4. The index you chose in the last is omitted. Hence in line #4, our printed string ended in the 7'th character. You can learn about these rules more by trying yourself.

Finally, to dig deeper, I give you the formula of indicing:

output_string = input_string[start_index : end_index : step]

But what about step? We have not used that before! As the name suggests, the step parameter will give you the characters combined in a string in the domain of given start end indices in each step that any character occur. For example:

And the output will be:

And last but not least, you can easily reverse a string by using this indicing ability such as:

And the output:

You can see, I left the start and the end part blank in the expression. If you leave any of them blank, then the indexing operator works with “rest” logic. Hence, in English, I said to the computer that print my string from starting to the rest by stepping backwards.

That is all for this article! Next time, we will dive in to numerics in Python! See you next time!

--

--

Ali Taha Dinçer
The Startup

Native and Cross platform mobile application developer. Bilkent University CS Grad. Experienced drummer. Photographer. Writer.