41 Questions To Test Your Knowledge of Python Strings PDF
41 Questions To Test Your Knowledge of Python Strings PDF
Strings
How to crush algorithm questions by mastering string fundamentals
Chris
Apr 28 · 8 min read
I’ve started tracking the most commonly used functions while doing algorithm
questions on LeetCode and HackerRank.
Being a good engineer isn’t about memorizing a language’s functions, but that doesn’t
mean it’s not helpful. Particularly in interviews.
You have two free stories left this month.
Upgrade for unlimited access
This is my string cheatsheet converted into a list of questions to quiz myself. While
these are not interview questions, mastering these will help you solve live coding
questions with greater ease.
. . .
1. How would you confirm that 2 strings have the same identity?
The is operator returns True if 2 names point to the same location in memory. This is
what we’re referring to when we talk about identity.
Don’t confuse is with ==, the latter which only tests equality.
animals = ['python','gopher']
more_animals = animals
even_more_animals = ['python','gopher']
Notice above how animals and even_more_animals have a different identity even
though they are equal.
Additionally, the id() function returns the id of a memory address associated with a
name. Two objects with the same identity will return the same id .
name = 'object'
id(name)
#=> 4408718312
2. How would you check if each word in a string begins with a capital
letter?
You have two free stories left this month.
Upgrade for unlimited access
The istitle() function checks if each word is capitalized.
name = 'Chris'
food = 'creme brulee'
You'the
have two free stories
happiest left this in
person month.
the whole wide world.'.index('the')
Upgrade
#=> 0for unlimited access
10. Interpolate a variable into a string using format()
format() is similar to using an f-string. Though in my opinion, it’s less user friendly
because variables are all passed in at the end of the string.
difficulty = 'easy'
thing = 'exam'
'not--so--great'.split('--')
#=> ['not', 'so', 'great']
''.join(reversed("hello world"))
#=> 'dlrow olleh'
'-'.join(['a','b','c'])
#=> 'a-b-c'
animal = 'fish'
sentence.splitlines()
#=> ['It was a stormy night', 'The house creeked', 'The wind blew.']
You have two free stories left this month.
Upgrade for unlimited access
23. Give an example of string slicing
Slicing a string takes up to 3 arguments, string[start_index:end_index:step] .
step is the interval at which characters should be returned. So a step of 3 would return
the character at every 3rd index.
'One1'.isalpha()
'One'.isalpha()
sentence.replace('sea', 'mountain')
#=> 'Sally sells mountain shells by the mountain shore'
29. Remove whitespace from the left, right or both sides of a string
lstrip() , rstrip() and strip() remove whitespace from the ends of a string.
'Fresh Tuna'.encode('ascii')
#=> b'Fresh Tuna'
'dog' * 3
# 'dogdogdog'
proverb_two = 'Rise each day before the sun' + ' if its a weekday'
print( id(proverb_two) )
#=> 4442287440
the object was actually modified then it would have the same id .
It only creates one. I found this unintuitive the first time I came across it. But this helps
python save memory when dealing with large strings.
We’ll prove this with id() . Notice how both have the same id .
animal = 'dog'
print( id(animal) )
#=> 4441985688
pet = 'dog'
print( id(pet) )
#=> 4441985688
39. Givetwoanfreeexample
You have stories left of
thisusing
month. maketrans() and translate()
Upgrade for unlimited access
maketrans() creates a mapping from characters to other characters. translate() then
applies that mapping to translate a string.
# create mapping
mapping = str.maketrans("abcs", "123S")
# translate string
"abc are the first three letters".translate(mapping)
#=> '123 1re the firSt three letterS'
Notice above how we changed the values of every a, b, c and s in the string.
vowels = ('a','e','i','o','u')
. . .
Conclusion
As
YouIhave
often explained
two free storiesto anthis
left old product manager, engineers aren’t dictionaries of stored
month.
Upgrade forBut
methods. unlimited access a little less googling can make coding more seamless and
sometimes
enjoyable.
If you found it too easy, you may be interested in my other article, 54 Python Interview
Questions.