Every value that we work with in Python has a type. A data type is a classification that specifies which type of value a variable has and what type of operations can be applied to it. This post is a quick introduction to the String data type in python. Let’s get started!
An ordered sequence of characters is called a String. Anything under a
‘ ‘ single-quote or a
“ “ is a string. It is used to store sentences, words, characters, special symbols, spaces etc. A username is an example of where strings might be used in real life.
Declaring the String Data Type:
All you have to do is assign a variable name to the string of your choice in
‘’ or a
“”. And that’s pretty much it.
myStr = 'a'
myStr2 = "The quick brown fox jumps over the lazy dog"
myStr3 = "!@#$%^&*()_+"
myStr4 = '222'
Run the code below and see for yourself :
Any sequence of characters. ANY. Anything under a
‘ ‘ or a
“ “ is a string.
Even if it’s a number and it’s inside a
‘ ‘ it’s considered as a string. I repeat ANY. ANYTHING INSIDE IT.
You can also add strings using the + operator:
The code below is pretty self-explanatory. Run the code and see the results.
myStr = "Hello"
myStr2 = "World"
myStr3 = myStr + myStr2 + "!"
print(myStr + myStr2 + "!")
print("Hello" + myStr2 + "!")
print(myStr + "World" + "!")
print("Hello" + "World" + "!")
Because strings are an ordered sequence of characters we can index them. Meaning every character in the string has a number associated with it. Using these numbers we can grab parts of the string and we can manipulate them without affecting the original string. Let me show you what I’m talking about.
Let’s Look at an example of the string
“Lasagna”. Let’s declare it.
myString = "Lasagna" #This is a string
myString = 'Lasagna' # You can also use ' ' instead of " "
A String can be indexed in two ways:
- Positive Indexing
- Negative or reverse indexing
1. Positive Indexing:
In positive indexing, the string is numbered from left to right starting with 0. Yes 0. You start numbering from 0. So 0 will the index value of the first character in the string, 1 will be the index value of the second character and so on …
2. Negative Indexing:
In negative indexing, the string is numbered from the right to left start with negative 1. *For Obvious reasons. You do know right?*. So -1 will the be the index of the first character from the right and you increment as you go on …
So to grab a character from a string:
myString = "Lasagna"
a = myString # stores 'L' in a
b = myString # stores 'a' in b
c = myString[-1] # stores 'a' in c
d = myString[-5] # stores 's' in d
Edit the code below and try to grab ‘g’ and store it in a new variable
Any character in a string is indexable. Even spaces in between sentences. So even the spaces should be numbered and considered during indexing.
Okay so now what if we want to grab multiple characters? This is where slicing comes in.
In slicing, we’ll use indexing to grab a portion of the original string. Here’s the Syntax :
variableName[startIndex : stopIndex].
myString = "The Council of Water Sheep"
Say for example you want to grab the first 10 characters in the myString variable. All you have to do is mention the
start index followed by a
: then the
stop index. The stop index is excluding meaning it’ll grab the characters only until the stop index but not including the character at the stop index. The start index is inclusive.
So we can modify the syntax as follows:
variableName[startIndex : stopIndex + 1]. The + 1 is to include the character at the stop index. Just increment it to the next number. In the case of negative indexing if the index is -16 for example, incrementing it by one should give you -15 and so on...
This slicing operation isn’t visible in the output section unless you print it or store it in a variable and then print it.
So let’s try and grab the first 10 characters:
“The Council” :
Positive Indexing :
a = myString[0:11]
0 is the start of the index and 10 is the stop index for the substring we are trying to grab. But since the stop index isn’t inclusive we increment it to 11.
Negative Indexing :
-26 is the start index and -16 is the stop index. But we increment it to -15. *Why?*
We normally use negative indexing to access the characters towards the end of the string as it is easier to number. But you can use negative indexing to grab the characters at the beginning of the string as well.
Refer to the index mapping of the string in the image above to get a better understanding.
Now let’s grab ‘water’ from the string :
Positive Indexing :
Negative Indexing :
Run the code below to check it out!
Let’s grab the last 8 characters now :
Should work just fine. As expected.
myString[-8:-1] # Something's wrong I can feel it
Something’s wrong in the above line of code. The last character has the value of -1. You can’t increment it to 0 and expect it to return the desired outcome. It messes up the order. So you leave the stop index blank. Don’t freak out. Just keep reading.
Leaving the start or stop index blank:
Case 1: Stop index is blank:
If we leave the stop index blank then the slicing will grab all the characters from the start index till the end of the string. Let’s try and grab the ‘Sheep’ substring from
myString[21:] # Positive indexing
myString[-8:] # Negative indexing
21 / -8 is the start of the index for the substring ‘Sheep’. We specify the start string alone and leave the stop index blank so it’ll grab everything from 21 / -8 till the very end of the string.
Case 2: Start index is blank:
If we leave the start index blank then the slicing will grab all the characters from the start of the string till the stop index. Let’s grab the first 10 characters of
myString by leaving the start index blank.
myString[:10] # Positive indexing
myString[:-16] # Negative indexing
10 / -16 is the stop index to grab the first 10 characters from the string. We leave the start index blank. So this slicing operation grabs everything from the start of the string until the stop index.
Case 3: Both the Start and Stop index are blank:
If both the start and stop index is blank then the slicing operation will grab the entire string. Because there is no start or stop conditions mentions.
There’s this thing called the step index:
Optionally you can include this thing called the step value using which you can skip characters which are grabbed during the slicing operation. This step index is right after the stop index.
variableName[startIndex : stopIndex: stepValue]
So if we set the stop value as 2 it’s going to skip 2 characters for every 2 characters grabbed.
myString = "Encyclopaedia"
So the above code is going to grab the characters from the start index until the stop index 8 but only every 2 alternate characters.
So normally if the slicing was
myString[0:8] it would grab the substring :
Encyclop. But if we add the step value
myString[0:8:2] it would return
You can leave the start/stop/step values blank in any varying combinations. Let me show you what I’m talking about.
Run the code to see the results :
Well, I hope you understood slicing properly. We’ll be using this concept in other data types as well. So get comfortable with it. It isn’t going anywhere.
If you’ve reached till here, GOOD JOB! Baby steps! One at a time! In this post, we learned about the Strings Data Type, how to declare it, add strings together, indexing, and slicing. We have some useful methods that we can apply on strings. We’ll look at them in another post. We need to learn a few more concepts first.
And that’s pretty much it for the Strings Data Type in Python. What? I’m serious. This is all you need to know about String. For Now.
If you enjoyed this content, make sure you follow the publication for future posts.
Peace Out ✌️