Python Strings in under 10 minutes
Tags: python, tutorial, beginners
Published: 2020-08-08 11:22:27


In the [previous post](https://dev.to/gothamv/python-numbers-in-under-10-minutes-55da), we discussed the numbers data type. In this post, we'll discuss about the String data type.

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!

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

## Strings

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 :


{% replit @Gothamv/strings %}

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" + "!")


{% replit @Gothamv/string %}

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

## Indexing :

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:**

1. Positive Indexing

1. Negative or reverse indexing

![](https://cdn-images-1.medium.com/max/4124/1*9voko6oLixeUbvKWq1OIAA.jpeg)

### **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:

Syntax : variableName[index]

    myString = "Lasagna"
    a = myString[0]  # stores 'L' in a 
    b = myString[6]  # 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 e.


{% replit @Gothamv/strings1 %}

**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.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

## Slicing:

In slicing we’ll use **indexing** to grab a portions of the original string. Here’s the Syntax : variableName[startIndex : stopIndex].

    myString = "The Council of Water Sheep"

![](https://cdn-images-1.medium.com/max/7264/1*Fr6JUGl0DiwjyHmJ9XBanw.png)

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” :

![First 10 characters of myString](https://cdn-images-1.medium.com/max/4124/1*o0KZex8P-srQHsixjOCuvQ.jpeg)*First 10 characters of myString*

### Positive Indexing :

    a = myString[0:11]
    print(a) 

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 :

    myString[-26:-15]

-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 :

![WATER substring from myString](https://cdn-images-1.medium.com/max/4124/1*jjL6Pljn81xSamE4jnpgLQ.jpeg)*WATER substring from myString*

### Positive Indexing :

    myString[15:20]

### Negative Indexing :

    myString[-11:-6]

Run the code below to check it out!


{% replit @Gothamv/strings3 %}

### **Let’s grab the last 8 characters now :**

![The last 8 characters of myString](https://cdn-images-1.medium.com/max/4124/1*LtwBiS8pDMeTafVj2QjfVw.jpeg)*The last 8 characters of myString*

### Positive Indexing

    myString[18:26]

Should work just fine. As expected.

### Negative Indexing

    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.

    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 string until the stop index.

### Case 3 : Both 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.

    myString[:]


{% replit @Gothamv/strings4 %}

### 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.
Syntax : 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"
    myString[0:8:2]

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 Ecco.

You can leave the start/stop/step values blank in any varying combinations. Let me show you what I’m talking about.

    myString[0::3]
    myString[:6:2]
    myString[::2]

Run the code to see the results :


{% replit @Gothamv/strings5 %}

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.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Next up: [It's all about lists](https://dev.to/gothamv/python-lists-in-under-10-minutes-3nj3).