Change language

Working with strings in Python. Preparing for an interview: the basics

| | | | | | | | |

In this article, we'll break down string handling in Python from an unusual angle - through the eyes of an interviewer in a job interview. The information will be useful for the beginner as well as the confident Juno. In the first part we'll talk about basic operations. In the second, we'll look at examples of tasks and questions that you should be prepared for.

Read also part 2: preparing for an interview.

So, we're at a job interview, and I want to know if you know how to handle strings.

How do you concatenate two strings together?

>>> s = "abc" + "def"
>>> s += "xyz"

Elementary? Almost. The important thing to remember is that strings are immutable objects. Every time we talk about "changing" a string, we're technically creating a new object and writing the calculated value there.

But how do you glue three strings together? The answer is "exactly the same", and sometimes this is the best way. But the interviewer probably wants to check if you know about the .join() method.

>>> names = ["John", "Paul", "Ringo", "George"]
>>> ", ".join(names)
'John, Paul, Ringo, George'

join() is a very convenient method that allows you to join N strings together, and with an arbitrary separator.

It is important not only to get a result, but also to understand how it works. Specifically, join() is a method of a "string" object that takes a list as an argument and returns a new string. By the way, a good task for an interview is to write your own implementation of join().

Split strings?

There are several ways to get part of a string. The first is split, the inverse of join. Unlike join, it applies to the target string and the separator is passed as an argument.

>>> s = "Alpha, Beta, Gamma"
>>> s.split(", ")
['Alpha', 'Beta', 'Gamma']

The second is via slices.

Slices s[x:y] allows you to have a substring from x to y. You can omit either value to move from the beginning or to the end of the string. Negative values are used to count down from the end (-1 being the last character, -2 being the penultimate, etc.).

>>> s = "Hello, world!"
>>> print(s[0:5])
Hello
>>> print(s[-6:])
world!

An optional third parameter s[x:y:N] can be used to select every Nth character from a substring. For example, get only even or only odd characters:

>>> s = "0123456789"
>>> print(s[::2])
02468
>>> print(s[1::2])
13579

What about searching in a string?

The quickest thing is to check if the string starts (ends) with the selected characters. Python has special string methods for this.

>>> s = "0123456789"
>>> s.startswith("012")
True
>>> s.endswith("69")
False

To search for a substring at an arbitrary location, there is a method called find(). It will return the index of the beginning of the found substring in the string, or -1 if nothing is found.

>>> s = "0123456789"
>>> s.find("45")
4
>>> s.find("42")
-1

For complicated cases, when one wants to find a pattern instead of a character string, regular expressions are useful. They deserve a separate article. You do not need a deep knowledge of regular expressions for job interviews, all you need is to know about them and be able to write a simple expression and read a little more complicated one. For example, this one:

>>> import re
>>> s = "https://www.reg.ru/hosting/";
>>> result = re.match(r"^(http|https)://([^/]+)(.*)$", s)
>>> print(result.group(1))
https
>>> print(result.group(2))
www.reg.ru
>>> print(result.group(3))
/hosting/

Can you make substitutions in a string?

First of all, you can replace anything with slices and concatenation of strings.

>>> s = "Hello, darling! How are you?"
>>> s[:7] + "Andrew" + s[14:]
'Hello, Andrew! How are you?'

Second, if you can find(), you can also replace().

>>> s.replace("darling", "Andrew")
'Hello, Andrew! How are you?'

Third, you can solve any problem with regular expressions. Or get two problems 🙂 In case of replace you need re.sub() method.

>>> s = "https://www.amd.com/hosting/";
>>> import re
>>> print(re.sub('[a-z]', 'X', s))
XXXXX://XXX.XXX.XX/XXXXXXX/

Symbolic processing?

There are countless problems that can be solved by looping through a string. For example, count the number of letters "o".

>>> s = "Hello, world!"
>>> for c in s:
>>>     if c == "o":
>>>         counter += 1
>>> print(counter)
2

Sometimes it's more convenient to run through an index.

>>> for i in range(len(s)):
>>>     if s[i] == "o":
>>>         counter += 1

Remember that strings are immutable, so you can't replace the i-th character by the index, you have to create a new object:

>>> s[i] = "X"
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
>>> s[:i] + "X" + s[i+1:]
'HellX, world!'

Or we can convert the string to a list, do the processing, and then glue the list back together into a string:

>>> arr = list(s)
>>> "".join(arr)
'Hello, world!'

And string formatting?

A typical necessity in life is to form a string by substituting the result of a program into it. As of Python 3.6, this can be done with f-string:

>>> f"String '{s}' consists of {len(s)} symbols."
"String 'Hello, world!' consists of 13 symbols."

In older code, you may find alternative ways of doing this

>>> "String '%s' consists of %d symbols" % (s, len(s))
>>> "String '{}' consists of {} symbols".format(s, len(s))

Technically you could do with gluing, but it's less elegant, and you also have to make sure that all the pieces you glue together are strings. Not recommended:

>>> "String '" + s + "' consists of " + str(len(s)) + " symbols."
"String 'Hello, world!' consists of 13 symbols."

/The employer's aim in an interview is to make sure you're savvy and that you can handle real-world tasks. However, immersion in real-world tasks takes several weeks and interview time is limited. That's why there are tutorials that can be done in 10-30 minutes, as well as questions to understand how the code works. We will talk about them in the next part.