Strings 3 - methods

Download exercises zip

Browse files online

Every data type has associated particular methods for that type, let’s see those associated to type string (str)

WARNING: ALL string methods ALWAYS generate a NEW string

The original string object is NEVER changed (because strings are immutable).

Result

Method

Meaning

str

str.upper()

Return the string with all characters uppercase

str

str.lower()

Return the string with all characters lowercase

str

str.capitalize()

Return the string with the first uppercase character

str

str.strip(str)

Remove strings from the sides

str

str.lstrip(str)

Remove strings from left side

str

str.rstrip(str)

Remove strings from right side

str

str.replace(str, str)

Substitute substrings

bool

str.startswith(str)

Check if the string begins with another one

bool

str.endswith(str)

Check whether the string ends with another one

int

str.find(str)

Return the first position of a substring starting from the left

int

str.rfind(str)

Return the first position of a substring starting from the right

int

str.count(str)

Count the number of occurrences of a substring

bool

str.isalpha(str)

Check if all characters are alhpabetic

bool

str.isdigit(str)

Check if all characters are digits

bool

str.isupper

Check if all characters are uppercase

bool

str.islower

Check if all characters are lowercase

Note: the list is not exhaustive, here we report only the ones we use in the book. For the full list see Python documentation

What to do

  1. Unzip exercises zip in a folder, you should obtain something like this:

strings
    strings1.ipynb
    strings1-sol.ipynb
    strings2.ipynb
    strings2-sol.ipynb
    strings3.ipynb
    strings3-sol.ipynb
    strings4.ipynb
    strings4-sol.ipynb
    jupman.py

WARNING: to correctly visualize the notebook, it MUST be in an unzipped folder !

  1. open Jupyter Notebook from that folder. Two things should open, first a console and then browser. The browser should show a file list: navigate the list and open the notebook strings3.ipynb

  2. Go on reading the exercises file, sometimes you will find paragraphs marked Exercises which will ask to write Python commands in the following cells. Exercises are graded by difficulty, from one star ✪ to four ✪✪✪✪

Shortcut keys:

  • to execute Python code inside a Jupyter cell, press Control + Enter

  • to execute Python code inside a Jupyter cell AND select next cell, press Shift + Enter

  • to execute Python code inside a Jupyter cell AND a create a new cell aftwerwards, press Alt + Enter

  • If the notebooks look stuck, try to select Kernel -> Restart

Example - upper

A method is a function of an object that takes as input the object to which is it is applied and does some calculation.

The type of the string (str) has predefined methods like str.upper() which can be applied to other string objects (i.e.: 'hello' is a string object)

The method str.upper() takes the string to which it is applied, and creates a NEW string in which all the characters are in uppercase. To apply a method like str.upper() to the particular string object 'hello', we must write:

'hello'.upper()

Frst we write the object on which apply the method ('hello'), then a dot ., and afterwards the method name followed by round parenthesis. The brackets can also contain further parameters according to the method.

Examples:

[2]:
'hello'.upper()
[2]:
'HELLO'
[3]:
"I'm important".upper()
[3]:
"I'M IMPORTANT"

WARNING: like ALL string methods, the original string object on which the method is called does NOT get modified.

Example:

[4]:
x = "hello"
y = x.upper()    # generates a NEW string and associates it to the variables y
[5]:
x                # x variable is still associated to the old string
[5]:
'hello'
[6]:
y                #  y variable is associated to the new string
[6]:
'HELLO'

Have a look now at the same example in Python Tutor:

[7]:
x = "hello"
y = x.upper()
print(x)
print(y)

jupman.pytut()
hello
HELLO
[7]:

Exercise - walking

Write some code which given a string x (i.e.: x='walking') prints twice the row:

walking WALKING walking WALKING
walking WALKING walking WALKING
  • DO NOT create new variables

  • your code must work with any string

[8]:
x = 'walking'

print(x, x.upper(), x, x.upper())
print(x, x.upper(), x, x.upper())
walking WALKING walking WALKING
walking WALKING walking WALKING

Help: If you are not sure about a method (for example, strip), you can ask Python for help this way:

WARNING: when using help, DON’T put parenthesis after the method name !!

[9]:
help("hello".strip)
Help on built-in function strip:

strip(...) method of builtins.str instance
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.

lower method

Return the string with all lowercase characters

[10]:
my_string = "HEllo WorLd"

another_string = my_string.lower()

print(another_string)
hello world
[11]:
print(my_string)  # didn't change
HEllo WorLd

Exercise - lowermid

Write some code that given any string x of odd length, prints a new string like x having the mid-character as lowercase.

  • your code must work with any string !

  • HINT: to calculate the position of the mid-character, use integer division with the operator //

Example 1 - given:

x = 'ADORATION'

it should print:

ADORaTION

Example 2 - given:

x = 'LEADINg'

it should print:

LEAdINg

[12]:
    #012345678
x = 'ADORATION'
#x = 'LEADINg'
k = len(x) // 2

print(x[:k] + x[k].lower() + x[k+1:])
ADORaTION

capitalize method

capitalize() creates a NEW string having only the FIRST character as uppercase:

[13]:
"artisan".capitalize()
[13]:
'Artisan'
[14]:
"premium".capitalize()
[14]:
'Premium'
[15]:
x = 'goat'
y = 'goat'.capitalize()
[16]:
x      #  x remains associate to the old value
[16]:
'goat'
[17]:
y      #  y is associated to the new string
[17]:
'Goat'

Exercise - Your Excellence

Write some code which given two strings x and y returns the two strings concatenated, separating them with a space and both as lowercase except the first two characters which must be uppercase

Example 1 - given:

x = 'yoUR'
y = 'exCelLenCE'

it must print:

Your Excellence

Example 2 - given:

x = 'hEr'
y = 'maJEsty'

it must print:

Her Majesty
Show solution
[18]:
x,y = 'yoUR','exCelLenCE'
#x,y = 'hEr','maJEsty'

# write here


Your Excellence

strip method

Eliminates white spaces, tabs and linefeeds from the sides of the string. In general, this set of characters is called blanks.

NOTE: it does NOT removes blanks inside string words! It only looks on the sides.

[19]:
x = ' \t\n\n\t carpe diem \t  '   # we put white space, tab and line feeds at the sides
[20]:
x
[20]:
' \t\n\n\t carpe diem \t  '
[21]:
print(x)


         carpe diem
[22]:
len(x)   # remember that special characters like \t and \n occupy 1 character
[22]:
20
[23]:
y = x.strip()
[24]:
y
[24]:
'carpe diem'
[25]:
print(y)
carpe diem
[26]:
len(y)
[26]:
10
[27]:
x      # IMPORTANT: x is still associated to the old string !
[27]:
' \t\n\n\t carpe diem \t  '

Specificying character to strip

If you only want Python to remove some specific character, you can specify them in parenthesis. Let’s try to specify only one:

[28]:
'salsa'.strip('s')    #  not internal `s` is not stripped
[28]:
'alsa'

If we specify two or more, Python removes all the characters it can find from the sides

Note the order in which you specify the characters does not matter:

[29]:
'caustic'.strip('aci')
[29]:
'ust'

WARNING: If you specify characters, Python doesn’t try anymore to remove blanks!

[30]:
'bouquet  '.strip('b')    # it won't strip right spaces !
[30]:
'ouquet  '
[31]:
'\tbouquet  '.strip('b')    # ... nor strip left blanks such as tab
[31]:
'\tbouquet  '

According to the same principle, if you specify a space ' ', then Python will only remove spaces and won’t look for other blanks!!

[32]:
'  careful! \t'.strip(' ')   # strips only on the left!
[32]:
'careful! \t'

QUESTION: for each of the following expressions, try to guess which result it produces (or if it gives an error):

  1. '\ttumultuous\n'.strip()
    
  2. ' a b c '.strip()
    
  3. '\ta\tb\t'.strip()
    
  4. '\\tMmm'.strip()
    
  5. 'sky diving'.strip('sky')
    
  6. 'anacondas'.strip('sad')
    
  7. '\nno way '.strip(' ')
    
  8. '\nno way '.strip('\\n')
    
  9. '\nno way '.strip('\n')
    
  10. 'salsa'.strip('as')
    
  11. '\t ACE '.strip('\t')
    
  12. ' so what? '.strip("")
    
  13. str(-3+1).strip("+"+"-")
    

lstrip method

Eliminates white spaces, tab and line feeds from left side of the string.

NOTE: does NOT remove blanks between words of the string! Only those on left side.

[33]:
x = '\n \t the street \t '
[34]:
x
[34]:
'\n \t the street \t '
[35]:
len(x)
[35]:
17
[36]:
y = x.lstrip()
[37]:
y
[37]:
'the street \t '
[38]:
len(y)
[38]:
13
[39]:
x       # IMPORTANT: x is still associated to the old string !
[39]:
'\n \t the street \t '

rstrip method

Eliminates white spaces, tab and line feeds from left side of the string.

NOTE: does NOT remove blanks between words of the string! Only those on right side.

[40]:
x = '\n \t the lighthouse \t '
[41]:
x
[41]:
'\n \t the lighthouse \t '
[42]:
len(x)
[42]:
21
[43]:
y = x.rstrip()
[44]:
y
[44]:
'\n \t the lighthouse'
[45]:
len(y)
[45]:
18
[46]:
x       # IMPORTANT: x è is still associated to the old string !
[46]:
'\n \t the lighthouse \t '

Exercise - hatespace

Given a string x which may contain some blanks (spaces, control characters like \t and \n, …) from begin to end, write some code which prints the string without blanks and the strings 'START' and 'END' at the sides

Example - given:

x = ' \t  \n \n hatespace\n   \t \n'

prints

STARThatespaceEND
Show solution
[47]:
# write here


STARThatespaceEND

Exercise - Bad to the bone

You have an uppercase string s which contains at the sides some stuff you want to remove: punctuation , a lowercase char and some blanks. Write some code to perform the removal

Example - given:

char = 'b'
punctuation = '!?.;,'
s = ' \t\n...bbbbbBAD TO THE BONE\n!'

your code should show:

'BAD TO THE BONE'
  • use only strip (or lstrip and rstrip) methods (if necessary, you can do repeated calls)

Show solution
[48]:
char = 'b'
punctuation = '!?.;,'
s = ' \t\n...bbbbbBAD TO THE BONE\n!'

# write here


[48]:
'BAD TO THE BONE'

replace method

str.replace takes two strings and looks in the string on which the method is called for occurrences of the first string parameter, which are substituted with the second parameter. Note it gives back a NEW string with all substitutions performed.

Example:

[49]:
"the train runs off the tracks".replace('tra', 'ra')
[49]:
'the rain runs off the racks'
[50]:
"little beetle".replace('tle', '')
[50]:
'lit bee'
[51]:
"talking and joking".replace('ING', 'ed')  # it's case sensitive
[51]:
'talking and joking'
[52]:
"TALKING AND JOKING".replace('ING', 'ED')  # here they are
[52]:
'TALKED AND JOKED'

As always with strings, replace DOES NOT modify the string on which it is called:

[53]:
x = "On the bench"
[54]:
y = x.replace('bench', 'bench the goat is alive')
[55]:
y
[55]:
'On the bench the goat is alive'
[56]:
x  # IMPORTANT: x is still associated to the old string !
[56]:
'On the bench'

If you give an optional third argument count, only the first count occurrences will be replaced:

[57]:
"TALKING AND JOKING AND LAUGHING".replace('ING', 'ED', 2)  # replaces only first 2 occurrences
[57]:
'TALKED AND JOKED AND LAUGHING'

QUESTION: for each of the following expressions, try to guess which result it produces (or if it gives an error)

  1. '$£ciao£$'.replace('£','').replace('$','')
    
  2. '$£ciao£$'.strip('£').strip('$')
    

Exercise - substitute

Given a string x, write some code to print a string like x but with all occurrences of bab substituted by dada

Example - given:

x = 'kljsfsdbabòkkrbabej'

it should print

kljsfsddadaòkkrdadaej
Show solution
[58]:
# write here


kljsfsddadaòkkrdadaej

startswith method

str.startswith takes as parameter a string and returns True if the string before the dot begins with the string passed as parameter. Example:

[59]:
"the dog is barking in the road".startswith('the dog')
[59]:
True
[60]:
"the dog is barking in the road".startswith('is barking')
[60]:
False
[61]:
"the dog is barking in the road".startswith('THE DOG')  # uppercase is different from lowercase
[61]:
False
[62]:
"THE DOG BARKS IN THE ROAD".startswith('THE DOG')       # uppercase is different from lowercase
[62]:
True

Exercise - by Jove

Write some code which given any three strings x, y and z, prints True if both x and y start with string z, otherwise prints False

Example 1 - given:

x = 'by Jove'
y = 'by Zeus'
z = 'by'

it should print:

True

Example 2 - given:

x = 'by Jove'
y = 'by Zeus'
z = 'from'

it should print:

False

Example 3 - given:

x = 'from Jove'
y = 'by Zeus'
z = 'by'

it should print:

False
Show solution
[63]:
x,y,z = 'by Jove','by Zeus','by'     # True
#x,y,z = 'by Jove','by Zeus','from'  # False
#x,y,z = 'from Jove','by Zeus','by'  # False

# write here


True

endswith method

str.endswith takes as parameter a string and returns True if the string before the dot ends with the string passed as parameter. Example:

[64]:
"My best wishes".endswith('st wishes')
[64]:
True
[65]:
"My best wishes".endswith('best')
[65]:
False
[66]:
"My best wishes".endswith('WISHES')    # uppercase is different from lowercase
[66]:
False
[67]:
"MY BEST WISHES".endswith('WISHES')    # uppercase is different from lowercase
[67]:
True

Exercise - Snobbonis

Given couple names husband and wife, write some code which prints True if they share the surname, False otherwise.

  • assume the surname is always at position 9

  • your code must work for any couple husband and wife

Show solution
[68]:
                 #0123456789               #0123456789
husband, wife  = 'Antonio  Snobbonis',     'Carolina Snobbonis'   # True
#husband, wife = 'Camillo  De Spaparanzi', 'Matilda  Degli Agi'   # False

# write here


True

count method

The method count takes a substring and counts how many occurrences are there in the string before the dot.

[69]:
"astral stars".count('a')
[69]:
3
[70]:
"astral stars".count('A')    # it's case sensitive
[70]:
0
[71]:
"astral stars".count('st')
[71]:
2

Optionally, you can pass a two other parameters to indicate an index to start counting from (included) and where to end (excluded):

[72]:
#012345678901
"astral stars".count('a',4)
[72]:
2
[73]:
#012345678901
"astral stars".count('a',4,9)
[73]:
1

Exercise - astro money

During 2020 lockdown while looking at the stars above you started feeling… waves. After some thinking, you decided THEY wanted to communicate with you so you you set up a dish antenna on your roof to receive messages from aliens. After months of apparent irrelevant noise, one day you finally receive a message you think you can translate. Aliens are obviously trying to tell you the winning numbers of lottery!

A message is a sequence of exactly 3 different character repetitions, the number of characters in each repetition is a number you will try at the lottery. You frantically start developing the translator to show these lucky numbers on the terminal.

Example - given:

s = '$$$$€€€€€!!'

it should print:

$ € !
4 5 2
  • IMPORTANT: you can assume all sequences have *different* characters

  • DO NOT use cycles nor comprehensions

  • for simplicity assume each character sequence has at most 9 repetitions

Show solution
[74]:
    #01234567890      # $ € !
s = '$$$$€€€€€!!'     # 4 5 2
                      # a b c
#s = 'aaabbbbbbccc'   # 3 6 3
                      # H A L
#s = 'HAL'            # 1 1 1

# write here


$ € !
4 5 2

find method

find returns the index of the first occurrence of some given substring:

[75]:
#0123456789012345
'bingo bongo bong'.find('ong')
[75]:
7

If no occurrence is found, it returns -1:

[76]:
#0123456789012345
'bingo bongo bong'.find('bang')
[76]:
-1
[77]:
#0123456789012345
'bingo bongo bong'.find('Bong')    #  case-sensitive
[77]:
-1

Optionally, you can specify an index from where to start searching (included):

[78]:
#0123456789012345
'bingo bongo bong'.find('ong',10)
[78]:
13

And also where to end (excluded):

[79]:
#0123456789012345
'bingo bongo bong'.find('g',4, 9)
[79]:
-1

Exercise - bananas

While exploring a remote tropical region, an ethologist discovers a population of monkeys which appear to have some concept of numbers. They collect bananas in the hundreds which are then traded with coconuts collected by another group. To comunicate the quantities of up to 999 bananas, they use a series of exactly three guttural sounds. The ethologist writes down the sequencies and formulates the following theory: each sound is comprised by a sequence of the same character, repeated a number of times. The number of characters in the first sequence is the first digit (the hundreds), the number of characters in the second sequence is the second digit (the decines), while the last sequence represents units.

Write some code which puts in variable bananas an integer representing the number.

For example - given:

s = 'bb bbbbb aaaa'

your code should print:

>>> bananas
254
>>> type(bananas)
int
  • IMPORTANT 1: different sequences may use the *same* character!

  • IMPORTANT 2: you cannot assume which characters monkeys will use: you just know each digit is represented by a repetition of the same character

  • DO NOT use cycles nor comprehensions

  • the monkeys have no concept of zero

Show solution
[80]:
    #0123456789012
s = 'bb bbbbb aaaa'     # 254
#s = 'ccc cc ccc'       # 323
#s = 'vvv rrrr ww'      # 342
#s = 'cccc h jjj'       # 413
#s = '🌳🌳🌳 🍌🍌🍌🍌🍌🍌 🐵🐵🐵🐵'  # 364  (you could get *any* weird character, also unicode ...)

# write here


rfind method

Like find method, but search starts from the right.

isalpha method

The method isalpha returns True if all characters in the string are alphabetic:

[81]:
'CoralReel'.isalpha()
[81]:
True

Numbers are not considered alphabetic:

[82]:
'Route 666'.isalpha()
[82]:
False

Also, blanks are not alphabetic:

[83]:
'Coral Reel'.isalpha()
[83]:
False

… nor punctuation:

[84]:
'!'.isalpha()
[84]:
False

… nor weird Unicode stuff:

[85]:
'♥'.isalpha()
[85]:
False
[86]:
''.isalpha()
[86]:
False

isdigit method

isdigit method returns True if a string is only composed of digits:

[87]:
'391'.isdigit()
[87]:
True
[88]:
'400m'.isdigit()
[88]:
False

Floating point and scientific notations are not recognized:

[89]:
'3.14'.isdigit()
[89]:
False
[90]:
'4e29'.isdigit()
[90]:
False

isupper and islower methods

We can check wheter a character is uppercase or lowercase with isupper and islower methods:

[91]:
'q'.isupper()
[91]:
False
[92]:
'Q'.isupper()
[92]:
True
[93]:
'b'.islower()
[93]:
True
[94]:
'B'.islower()
[94]:
False

They also work on longer strings, checking if all characters meet the criteria:

[95]:
'GREAT'.isupper()
[95]:
True
[96]:
'NotSoGREAT'.isupper()
[96]:
False

Note blanks and punctuation are not taken into account:

[97]:
'REALLY\nGREAT !'.isupper()
[97]:
True

We could check whether a character is upper/lower case by looking at ASCII code but the best way we covers all alphabets is by using isupper and islower methods, for example they also work with accented letters:

[98]:
'à'.isupper()
[98]:
False
[99]:
'Á'.isupper()
[99]:
True

Other exercises

QUESTION: For each following expression, try to find the result

  1. 'gUrP'.lower() == 'GuRp'.lower()
    
  2. 'NaNo'.lower() != 'nAnO'.upper()
    
  3. 'O' + 'ortaggio'.replace('o','\t \n     ').strip() + 'O'
    
  4. 'DaDo'.replace('D','b') in 'barbados'
    

Continue

Go on reading notebook Strings 4 - other exercises

[ ]: