Python strings: Introduction

A string is a sequence of characters which is treated as a single data item. Python strings are array of characters or any group of characters defined between double quotes or single quotes.

For example, “This is a string” or ‘This is a string’.

The characters in the string are stored in the consecutive memory location as the string is treated as an array of characters. Each character occupies 1 byte in the memory heap.

Python strings are also immutable, meaning they cannot be modified once created.

Creating Python Strings

Creating strings in Python is as simple as assigning a value to the variable. The value that is being assigned must be in either within double quotes " " or single quotes ' ' for the string.

A string literal can be of multiple lines as well. In such cases, triple quotes are used.

Here are the examples to demonstrate Python strings.

>>> #using single quotes
>>> py_str = 'YOLO'
>>> print (py_str)
YOLO
>>> #using double quotes
>>> py_str = "Hey there"
>>> print (py_str)
Hey there
>>> #using triple quotes for multiple line strings
>>> py_str = """ This is first line
and this is second line."""
>>> print (py_str)
This is first line
and this is second line.

Here in the example, the length of the string is 9 because it also counts the whitespace in between.

How to access characters in a Python string?

Once we create Python string, we can’t modify them but we can access the characters in the string and use them in further operations.

In Python, individual characters or elements are accessed using indexing and a range of characters or string slices are accessed using slicing. Before jumping into examples, let us first know about indexing and slicing.

Before jumping into examples, let us first know about indexing and slicing.

What is indexing?

Indexing actually means locating an element from a Python sequence (list, string, tuple, dictionary or any) by its position in that sequence. This indexing starts from zero meaning the first element of the sequence holds the index '0'.

Python has indexing operator '[ ]'.

There are two ways we can index string or any sequence in Python:

index with positive integers: Index from left starting with 0 and 0 being the index of the first item in the string or sequence

index with negative integer: Index from the right starting with -1 and -1 being the index of the last item in the string or sequence.

For example, consider the following string with 4 characters.

py_str = "abcd"

Now, if we index with positive integers using the indexing operator '[ ]':

py_str[0] = a, py_str[1] = b, py_str[2] = c, py_str[3] = d

And if we index with negative integers, then

py_str[-1] = d, py_str[-2] = c, py_str[-3] = b, py_str[-4] = a

Note: Only integers are allowed in indexing.

Common Programming Errors

Trying to access 5th character in the string of 4 characters will raise an error saying: IndexError: String index out of range. And also using floating point numbers instead of integers to index will also raise an error saying: TypeError

What is Slicing in Python?

Slicing as the meaning goes is basically chopping or retrieving a set of values from a particular sequence.

In slicing we define a starting point to start retrieving the values, an endpoint to stop retrieving (the endpoint value is not included) and the step size. Step size is by default 1, if not mentioned explicitly.

And the retrieved set of characters from the string is known as Python slices.

py_ssequence[start:end:step_size]

Now that we know about indexing and slicing, let’s learn about using slicing and indexing to access characters from Python strings with examples.

To access single character: Indexing

py_str[index_char]

Where index_char is the index of the character to be accessed from string py_str.

For example, if we have a string named py_str, we can access its 5th character by using py_str[4] as the indexing starts from 0.

To access a range of character or a segment of the string: Slicing

py_str[m,n]

This will return characters starting from mth character to the nth character from the string excluding nth character.