In this article, I will explain, what string is, what the main commands are, and what escape sequences are, and where and how we use them. There are more to string and since it will be long, I decided to divide into a couple of articles. This article will be the first article of the string series.
Prerequisites:
Let´s start.
String: String data type is a sequence of the data. For example, if you have data like, let´s say, "I am a beginner". This is a string. Strings are a list of characters. Those characters can be letters, numbers, symbols, spaces, and escape characters. “str” represents a string. When you are using string, you have to use it between " " (double quotes) or ´ ´ (single quote). So far, it might be a little bit confusing. Let´s show an example for you to understand better.
Code:
>>> print("This is a string.")
Output:
This is a string.
Code:
>>> type("This is a string.")
Output:
<class 'str'>
As you see, letters are string. Can numbers be a string too? Yes. Let´s see.
Code:
>>> print("120")
Output:
120
Code:
>>> type("120")
Output:
<class 'str'>
I want you to understand two things very well here. First thing is that I used " " (double quotes). As I mentioned earlier, we have to use double or single quotes to specify the data as a string. Double quotes and single quotes are the same. The only thing is that, whichever you start with, you should finish with the same quotes. So far, it should be clear. Second thing is that when we use numbers as strings, also they are sequenced numbers. What do I mean? When we used to print("120"), since it is in parentheses, python will read it like this (one-two zero) instead of one hundred twenty.
Strings technically have no limits. The only limit is the user´s computer memory. It might also be empty.
If we want to print double quotes or single quotes what should we do?
Python has escape sequences in strings.
Let´s go to details of these escape sequences.
Escape Sequences:
There are many built-in functions in Python. Sometimes, there might be a problem like an example I gave before. However, there is also a solution for that. There are two different solutions to this specific problem.
- We can suppress the special interpretation
- We can apply a special interpretation to characters in a string.
A backslash (\) is called an escape character. I will not bore you trying to explain everything so deeply. I will share a table for your reference. All I ask you to try all of them. This is not a full list. The reason I did not give you the full list is that I want you to understand what and how to use rather than dumping a lot of information in front of you. If you would like to learn more about escape sequences, you may search google for a full list.
Escape Sequence (Code) | Description |
\' or \" | Inserts a single quote or double quote |
\\ | Inserts a backslash |
\n | Creates a new Line |
\r | Carriage Return |
\t | Inserts tab |
\b | Deletes last character (backspace) |
\ooo | Allows you to type an octal value |
\xhh | Allows you to type a hex value |