Strings in Python

Video Tutorial
FREE
Strings thumbnail
This video belongs to
Python and SQL for Data Science
8 modules
Certificate
Topics Covered

Overview

A string is just a sequence of characters. It is among the most popular data types in Python. It can be created simply by enclosing characters in quotes.

Python provides a rich set of operators, functions, and methods for working with strings, which helps access and extract portions of strings and manipulate and modify string data.

What is a string in python?

Technically speaking, an immutable data sequence is known as a string in Python. In simple words, as discussed in the case of a crossword, a Python string is nothing but an array of characters, but a computer does not understand characters. It only understands the language of 0’s and 1’s.

So these characters are converted to a number so the computer can understand them clearly. This conversion is known as encoding. ASCII and Unicode are some of the popular encodings used.

To put it simply, Python strings are a sequence of Unicode characters.

Creating a String in Python

A string in Python can be easily created using single, double, or even triple quotes. The characters that form the string are enclosed within any of these quotes.

Note: Triple quotes are generally used when we are working with multiline strings in Python and docstring in Python.

Let’s take a look at an example where we will create strings using the three types of quotes –

Code:

Output:

Reassigning Strings in Python

Since strings in Python are immutable, we can update the values of strings simply by reassigning these strings with new values. The strings cannot be partially replaced, so it is always replaced completely with a new string. Let’s see how that’s done –

Code:

Output:

From the output, it is evident that the string variable message was reassigned with a new string, which gets printed to the console in the last line.

How to access characters in a python string?

As we know, a string comprises a sequence of characters. Thus, in Python, we have some techniques to reference or index a particular character in a string.

The two primary ways of accessing characters of a string in Python are-

1. Indexing

The length of a string can be easily calculated using the len() function. The length of a string provides us with a range of the string. In python, we can perform indexing on any string as long as it is within the range. The index is always referred to as an integer. Using any other data type would give rise to a TypeError.

Indexing can either be positive or negative. Positive indexing starts from 0 and indexing from the beginning of the string. On the other hand, negative indexing starts from -1, and indexing starts from the end of the string.

characters of a string in python Come and explore how indexing works by looking at the following example –

Output

Using the len() function, we calculated the length of the string variable message. We performed positive and negative indexing on this Python string. The thing to note is that we got an IndexError when the index value was way beyond the range of the string.

2. Slicing

Unlike indexing, slicing returns a range of characters using the slicing syntax.

The syntax looks like this –

While performing slicing operations, one thing to remember is that the start index value is always included while that of the last index is always excluded.

Slicing can also be negative or positive, just like indexing in Python!

Let’s consider the following examples to understand this better –

Code:

Output:

So if we take a close look at the positive slicing performed by us, we can see that the character at the 2nd index is included, whereas at the 5th index is excluded, provided the indexing starts from the 0th position.

How to delete a string?

We have established that strings are immutable. In simple words, it means that once a string is assigned, we cannot make any changes to it. We cannot remove or delete any character of the string.

What we can do is delete the Python string entirely. We can do so by using the del keyword.

The following code snippet shows how we can do that –

The output is –

We can see that before we used del, the string was printed to the console. But as soon as we used the del keyword, the string variable was successfully deleted. Hence, the print function gives us an error.

String Operators in Python

Arithmetic operators do not work on strings. But, we do have certain operators for performing special operations on strings in Python.

Let’s check out some of them –

OperatorDescription
+It is used to concatenate two strings. It appends the second string to the first string.
*It concatenates multiple copies of the same string. It is basically a repetition operator.
[]It is used for indexing. The value put between this operator returns the character at the given index.
[:]It is used for slicing. It returns the sub-strings after slicing as per the given range.
inIt returns true if the character is present in the given string.
not inIt returns true if the character is not present in the given string.

Example Program of String Operators in Python

Now that we have learned some Python string operators let’s get coding and see how they work –

Code:

Output:

Conclusion

We have now reached the end of the article.

You learned a lot of things but most importantly –

  • You learned what strings in Python are.
  • You got to know how to create, access, reassign, and delete them.
  • You also became familiar with some of the popular string operators.

You are now well-equipped with enough knowledge about Python strings. So wait no more and get coding!

See Also: