Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get the length of a string in Python

Introduction

As a beginner in programming, you will often come across the need to manipulate strings. One of the most basic string manipulations is finding the length of a string. In this blog, we will explore how to get the length of a string in Python, understand the intuition behind it, and provide examples to help you comprehend the concept.

What is a String?

Before diving into finding the length of a string, let's quickly understand what a string is. In simple terms, a string is a sequence of characters. Characters can be alphabets, digits, or symbols. In Python, you can create a string by enclosing a sequence of characters within single quotes (') or double quotes (").

For example:

string1 = 'Hello, World!'
string2 = "Python is fun"

Intuition behind String Length

Now that we know what a string is, let's try to understand the intuition behind the length of a string. In the context of strings, length refers to the number of characters in the string. For example, the length of the string Hello is 5 because it consists of 5 characters: H, e, l, l, and o.

Consider the string as a chain of characters, where each character is a link in the chain. The length of the string is simply the count of links in the chain. Let's take a look at an analogy to make this more relatable. Imagine a necklace made of beads, with each bead representing a character in the string. The length of the necklace corresponds to the number of beads (characters) used to make it.

Using the len() Function

Now that we've understood the intuition behind the length of a string, let's see how to find it in Python. Python provides a built-in function called len() that returns the length of a given string. The syntax for using the len() function is as follows:

length = len(string)

Here, string is the string whose length you want to find, and length is a variable that will store the length of the string.

Let's see some examples:

string1 = 'Hello, World!'
length1 = len(string1)
print("The length of the string", string1, "is:", length1)

string2 = "Python is fun"
length2 = len(string2)
print("The length of the string", string2, "is:", length2)

Output:

The length of the string Hello, World! is: 13
The length of the string Python is fun is: 12

As you can see, the len() function returns the length of the string passed as an argument.

String Length and Indexing

Understanding the length of a string is crucial when dealing with string indexing. In Python, you can access individual characters in a string using their indices. Indices start at 0 and go up to the length of the string minus 1. For example, in the string Hello, the index of the character H is 0, and the index of the character o is 4 (which is equal to the length of the string minus 1).

Here's a visual representation of the indices of the string Hello:

 H  e  l  l  o
 0  1  2  3  4

Let's see an example:

string = "Hello"
first_char = string[0]
last_char = string[4]

print("The first character of the string is:", first_char)
print("The last character of the string is:", last_char)

Output:

The first character of the string is: H
The last character of the string is: o

You can also use negative indices to access the characters from the end of the string. For example, the index of the last character in the string Hello is -1, the second last character is -2, and so on.

Here's a visual representation of the negative indices of the string Hello:

 H  e  l  l  o
-5 -4 -3 -2 -1

Using negative indices in the previous example:

string = "Hello"
first_char = string[-5]
last_char = string[-1]

print("The first character of the string is:", first_char)
print("The last character of the string is:", last_char)

Output:

The first character of the string is: H
The last character of the string is: o

Knowing the length of a string is essential to avoid index errors while accessing characters in the string. For example, if you try to access an index equal to or greater than the length of the string, Python will raise an IndexError.

string = "Hello"
invalid_char = string[5]  # This will raise an IndexError

Conclusion

In this blog, we learned about strings in Python, the intuition behind the length of a string, and how to find the length of a string using the built-in len() function. We also discussed how the length of a string is related to string indexing, and how it helps avoid index errors.

Understanding string length and how to work with it is a fundamental building block in programming. As you move forward in your programming journey, you will frequently encounter situations where manipulating strings and their lengths becomes an essential part of your code. So, keep practicing and exploring the world of strings in Python!