1. Python strings are immutable sequences and can be indexed, sliced, and iterated like any other sequence, as well as being subject to the in and not in operators. There are two kinds of strings in Python:
- one-line strings, which cannot cross line boundaries – we denote them using either apostrophes (
'string') or quotes ("string") - multi-line strings, which occupy more than one line of source code, delimited by trigraphs:
|
1 2 3 |
''' string ''' |
or
|
1 2 3 |
""" string """ |
2. The length of a string is determined by the len() function. The escape character (\) is not counted. For example:
|
1 |
print(len("\n\n")) |
2
|
1 2 |
x="\\\\" print(len(x)) |
2
|
1 2 |
x="\\\" print(len(x)) |
x="\\\"
^
SyntaxError: unterminated string literal (detected at line 1)
\ escape only one character.
3. Strings can be concatenated using the + operator, and replicated using the * operator. For example:
|
1 2 3 4 |
asterisk = '*' plus = "+" decoration = (asterisk + plus) * 4 + asterisk print(decoration) |
outputs *+*+*+*+*.
4. The pair of functions chr() and ord() can be used to create a character using its codepoint, and to determine a codepoint corresponding to a character. Both of the following expressions are always true:
|
1 2 |
chr(ord(character)) == character ord(chr(codepoint)) == codepoint |
5. Some other functions that can be applied to strings are:
list() – create a list consisting of all the string’s characters;
max() – finds the character with the maximal codepoint;
min() – finds the character with the minimal codepoint.
6. The method named index() finds the index of a given substring inside the string and raises an exception if the value is not found.
The syntax is as follows :
string.index(value, start, end) where:
– value [required] : the string/character to search for
– start [optional] : the index to start the search (default is 0)
– end [optional] : the index to end the search (default is the end of the string) – note that this index is excluded from the search.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
my_str = 'Luke !!\n' z = my_str.index('n') print(z) # ValueError Exception x = my_str.index('L') print(x) # 0 y = my_str.index('!',4) print(y) # 5 w = my_str.index('!',1,5) print(w) # ValueError Exception |
In the above question, my_str.index('n') will return the index of the first occurrence of the letter “n” in the string my_str, searching between the character at position 0 and the last character of the string (i.e. the whole string). In the string ‘Luke !!\n’ , '\n' is actually the code for a new line (note that escape character ‘\’); this is not representing the character ‘n’. So character 'n' is NOT in the string ‘Luke !!\n’ and my_str.index(‘n’) will raise an exception.
1. Some of the methods offered by strings are:
capitalize()– changes all string letters to capitals;
|
1 2 3 4 5 6 7 |
# Demonstrating the capitalize() method: print('aBcD'.capitalize()) print("Alpha".capitalize()) print('ALPHA'.capitalize()) print(' Alpha'.capitalize()) print('123'.capitalize()) print("αβγδ".capitalize()) |
Abcd
Alpha
Alpha
alpha
123
Αβγδ
center()– centers the string inside the field of a known length;
|
1 2 3 4 5 |
# Demonstrating the center() method: print('[' + 'alpha'.center(10) + ']') print('[' + 'Beta'.center(2) + ']') print('[' + 'Beta'.center(4) + ']') print('[' + 'Beta'.center(6) + ']') |
[ alpha ]
[Beta]
[Beta]
[ Beta ]
The two-parameter variant of center() makes use of the character from the second argument, instead of a space. Analyze the example below:
|
1 |
print('[' + 'gamma'.center(20, '*') + ']') |
[*******gamma********]
count()– counts the occurrences of a given character;
join()– joins all items of a tuple/list into one string;
|
1 2 |
# Demonstrating the join() method: print(",".join(["omicron", "pi", "rho"])) |
omicron,pi,rho
|
1 2 3 |
myTuple = ("John", "Peter", "Vicky") x = "#".join(myTuple) print(x) |
John#Peter#Vicky
lower() – converts all the string’s letters into lower-case letters;
|
1 2 |
# Demonstrating the lower() method: print("SiGmA=60".lower()) |
sigma=60
lstrip() – removes the white characters from the beginning of the string;
|
1 2 |
# Demonstrating the lstrip() method: print("[" + " tau ".lstrip() + "]") |
[tau ]
The one-parameter lstrip() method does the same as its parameterless version, but removes all characters enlisted in its argument (a string), not just whitespaces:
|
1 |
print("www.cisco.com".lstrip("w.")) |
cisco.com
rstrip() – removes the trailing white spaces from the end of the string;
do nearly the same as lstrips, but affect the opposite side of the string.
|
1 2 3 |
# Demonstrating the rstrip() method: print("[" + " upsilon ".rstrip() + "]") print("cisco.com".rstrip(".com")) |
[ upsilon]
cis
strip() – removes the leading and trailing white spaces;
combines the effects caused by rstrip() and lstrip() – it makes a new string lacking all the leading and trailing whitespaces.
|
1 2 |
# Demonstrating the strip() method: print("[" + " aleph ".strip() + "]") |
[aleph]
|
1 2 3 |
txt = " banana " x = txt.strip() print("of all fruits", x, "is my favorite") |
of all fruits banana is my favorite
|
1 2 3 |
txt = ",,,,,rrttgg.....banana....rrr" x = txt.strip(",.grt") print(x) |
banana
split() – splits the string into a substring using a given delimiter;
|
1 2 |
# Demonstrating the split() method: print("phi chi\npsi".split()) |
['phi', 'chi', 'psi']
|
1 2 3 |
txt = "hello, my name is Peter, I am 26 years old" x = txt.split(", ") print(x) |
['hello', 'my name is Peter', 'I am 26 years old']
Note: the reverse operation can be performed by the join() method.
replace() – replaces a given substring with another;
|
1 2 3 4 |
# Demonstrating the replace() method: print("www.netacad.com".replace("netacad.com", "pythoninstitute.org")) print("This is it!".replace("is", "are")) print("Apple juice".replace("juice", "")) |
www.pythoninstitute.org
Thare are it!
Apple
The three-parameter replace() variant uses the third argument (a number) to limit the number of replacements.
|
1 2 3 |
txt = "one one was a race horse, two two was one too." x = txt.replace("one", "three", 2) print(x) |
three three was a race horse, two two was one too.
find() – it looks for a substring and returns the index of first occurrence of this substring. For an argument containing a non-existent substring (it returns -1 then)
|
1 2 3 |
# Demonstrating the find() method: print("Eta".find("ta")) print("Eta".find("mma")) |
1
-1
rfind() – finds a substring starting from the end of the string; string.rfind(value, start, end)
|
1 2 3 4 |
# Demonstrating the rfind() method: print("tau tau tau".rfind("ta")) print("tau tau tau".rfind("ta", 9)) print("tau tau tau".rfind("ta", 3, 9)) |
8
-1
4
|
1 2 3 |
txt = "Mi casa, su casa." x = txt.rfind("casa") print(x) |
12
|
1 2 3 |
txt = "Hello, welcome to my world." x = txt.rfind("e", 5, 10) print(x) |
8
swapcase() – swaps the letters’ cases (lower to upper and vice versa)
|
1 2 |
# Demonstrating the swapcase() method: print("I know that I know nothing.".swapcase()) |
i KNOW THAT i KNOW NOTHING.
title() – makes the first letter in each word upper-case;
|
1 2 |
# Demonstrating the title() method: print("I know that I know nothing. Part 1.".title()) |
I Know That I Know Nothing. Part 1.
upper() – converts all the string’s letter into upper-case letters.
|
1 2 |
# Demonstrating the upper() method: print("I know that I know nothing. Part 2.".upper()) |
I KNOW THAT I KNOW NOTHING. PART 2.
2. String content can be determined using the following methods (all of them return Boolean values):
endswith() – does the string end with a given substring?
|
1 2 3 4 5 6 7 8 9 10 11 |
# Demonstrating the endswith() method: if "epsilon".endswith("on"): print("yes") else: print("no") t = "zeta" print(t.endswith("a")) print(t.endswith("A")) print(t.endswith("et")) print(t.endswith("eta")) |
yes
True
False
False
True
startswith() – does the string begin with a given substring?
The startswith() method is a mirror reflection of endswith() – it checks if a given string starts with the specified substring.
|
1 2 3 |
# Demonstrating the startswith() method: print("omega".startswith("meg")) print("omega".startswith("om")) |
False
True
isalnum() – does the string consist only of letters and digits?
|
1 2 3 4 5 6 7 |
# Demonstrating the isalnum() method: print('lambda30'.isalnum()) print('lambda'.isalnum()) print('30'.isalnum()) print('@'.isalnum()) print('lambda_30'.isalnum()) print(''.isalnum()) |
True
True
True
False
False
False
|
1 2 3 4 5 6 7 8 |
t = 'Six lambdas' print(t.isalnum()) t = 'ΑβΓδ' print(t.isalnum()) t = '20E1' print(t.isalnum()) |
False (the cause of the first result is a space – it’s neither a digit nor a letter.)
True
True
isalpha() – does the string consist only of letters?
|
1 2 3 |
# Example 1: Demonstrating the isapha() method: print("Moooo".isalpha()) print('Mu40'.isalpha()) |
True
False
isdigit() – looks at digits only – anything else produces False as the result.
|
1 2 3 |
# Example 2: Demonstrating the isdigit() method: print('2018'.isdigit()) print("Year2019".isdigit()) |
True
False
islower() – does the string consists only of lower-case letters?
|
1 2 3 |
# Demonstrating the islower() method: print("Moooo".islower()) print('moooo'.islower()) |
False
True
isupper() – does the string consists only of upper-case letters?
|
1 2 3 4 |
# Demonstrating the isupper() method: print("Moooo".isupper()) print('moooo'.isupper()) print('MOOOO'.isupper()) |
False
False
True
isspace() – does the string consists only of white spaces?
|
1 2 3 4 |
# Example 2: Demonstrating the isspace() method: print(' \n '.isspace()) print(" ".isspace()) print("mooo mooo mooo".isspace()) |
True
True
False
Exercise 1
What is the length of the following string assuming there is no whitespaces between the quotes?
|
1 2 |
""" """ |
1
Exercise 2
What is the expected output of the following code?
|
1 2 3 |
s = 'yesteryears' the_list = list(s) print(the_list[3:6]) |
['t', 'e', 'r']
Exercise 3
What is the expected output of the following code?
|
1 2 |
for ch in "abc": print(chr(ord(ch) + 1), end='') |
bcd
Exercise 4
What is the expected output of the following code?
|
1 2 3 4 5 6 7 |
for ch in "abc123XYX": if ch.isupper(): print(ch.lower(), end='') elif ch.islower(): print(ch.upper(), end='') else: print(ch, end='') |
ABC123xyx
Exercise 5
What is the expected output of the following code?
|
1 2 3 |
s1 = 'Where are the snows of yesteryear?' s2 = s1.split() print(s2[-2]) |
of
Exercise 6
What is the expected output of the following code?
|
1 2 3 |
the_list = ['Where', 'are', 'the', 'snows?'] s = '*'.join(the_list) print(s) |
Where*are*the*snows?
Exercise 7
What is the expected output of the following code?
|
1 2 3 |
s = 'It is either easy or impossible' s = s.replace('easy', 'hard').replace('im', '') print(s) |
It is either hard or possible
Comparing strings
1. Strings can be compared to strings using general comparison operators, but comparing them to numbers gives no reasonable result, because no string can be equal to any number. Comparing strings against numbers is generally a bad idea. For example:
-
string == numberis always False;string != numberis always True;string >= numberalways raises an TypeError exception.
|
1 |
'alpha' < 'alphabet' |
True
String comparison is always case-sensitive (upper-case letters are taken as lesser than lower-case).
|
1 |
'beta' > 'Beta' |
True
Let’s check it:
|
1 2 3 4 5 |
'10' == 10 '10' != 10 '10' == 1 '10' != 1 '10' > 10 |
The results in this case are:
False
True
False
True
TypeError exception
2. Sorting lists of strings can be done by:
-
- a function named
sorted(), creating a new, sorted list;
- a function named
|
1 2 3 4 5 6 |
# Demonstrating the sorted() function: first_greek = ['omega', 'alpha', 'pi', 'gamma'] first_greek_2 = sorted(first_greek) print(first_greek) print(first_greek_2) |
['omega', 'alpha', 'pi', 'gamma']
['alpha', 'gamma', 'omega', 'pi']
-
- a method named
sort(), which sorts the list in place
- a method named
|
1 2 3 4 5 6 |
# Demonstrating the sort() method: second_greek = ['omega', 'alpha', 'pi', 'gamma'] print(second_greek) second_greek.sort() print(second_greek) |
['omega', 'alpha', 'pi', 'gamma']
['alpha', 'gamma', 'omega', 'pi']
3. A number can be converted to a string using the str() function.
|
1 2 3 4 5 6 |
itg = 13 flt = 1.3 si = str(itg) sf = str(flt) print(si + ' ' + sf) |
The code outputs:
13 1.3
4. A string can be converted to a number (although not every string) using either the int() or float() function. The conversion fails if a string doesn’t contain a valid number image (an exception is raised then).
|
1 2 3 4 5 6 |
si = '13' sf = '1.3' itg = int(si) flt = float(sf) print(itg + flt) |
This is what you’ll see in the console:
14.3
Exercise 1
Which of the following lines describe a true condition?
'smith' > 'Smith'
'Smiths' < 'Smith'
'Smith' > '1000'
'11' < '8'
1, 3 and 4
Exercise 2
What is the expected output of the following code?
|
1 2 3 4 5 |
s1 = 'Where are the snows of yesteryear?' s2 = s1.split() s3 = sorted(s2) print(s3[1]) print(s3) |
are
['Where', 'are', 'of', 'snows', 'the', 'yesteryear?']
Exercise 3
What is the expected result of the following code?
|
1 2 3 4 5 |
s1 = '12.8' i = int(s1) s2 = str(i) f = float(s2) print(s1 == s2) |
The code raises a ValueError exception because you can’t int('12.8')
The Caesar Cipher: encrypting a message
This cipher was (probably) invented and used by Gaius Julius Caesar and his troops during the Gallic Wars. The idea is rather simple – every letter of the message is replaced by its nearest consequent (A becomes B, B becomes C, and so on). The only exception is Z, which becomes A.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Caesar cipher. text = input("Enter your message: ") cipher = '' for char in text: if not char.isalpha(): continue char = char.upper() code = ord(char) + 1 if code > ord('Z'): code = ord('A') cipher += chr(code) print(cipher) |
The Caesar Cipher: decrypting a message
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Caesar cipher - decrypting a message. cipher = input('Enter your cryptogram: ') text = '' for char in cipher: if not char.isalpha(): continue char = char.upper() code = ord(char) - 1 if code < ord('A'): code = ord('Z') text += chr(code) print(text) |
The Numbers Processor
The third program shows a simple method allowing you to input a line filled with numbers, and to process them easily. The processing will be extremely easy – we want the numbers to be summed.
|
1 2 3 4 5 6 7 8 9 10 11 |
# Numbers Processor. line = input("Enter a line of numbers - separate them with spaces: ") strings = line.split() total = 0 try: for substr in strings: total += float(substr) print("The total is:", total) except: print(substr, "is not a number.") |
Enter a line of numbers - separate them with spaces: 12 4
The total is: 16.0
The IBAN Validator
The fourth program implements (in a slightly simplified form) an algorithm used by European banks to specify account numbers. The standard named IBAN (International Bank Account Number) provides a simple and fairly reliable method for validating account numbers against simple typos that can occur during rewriting of the number.
The standard says that validation requires the following steps (according to Wikipedia):
- (step 1) Check that the total IBAN length is correct as per the country (this program won’t do that, but you can modify the code to meet this requirement if you wish; note: you have to teach the code all the lengths used in Europe)
- (step 2) Move the four initial characters to the end of the string (i.e., the country code and the check digits)
- (step 3) Replace each letter in the string with two digits, thereby expanding the string, where A = 10, B = 11 … Z = 35;
- (step 4) Interpret the string as a decimal integer and compute the remainder of that number by modulo-dividing it by 97; If the remainder is 1, the check digit test is passed and the IBAN might be valid.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# IBAN Validator. iban = input("Enter IBAN, please: ") iban = iban.replace(' ','') if not iban.isalnum(): print("You have entered invalid characters.") elif len(iban) < 15: print("IBAN entered is too short.") elif len(iban) > 31: print("IBAN entered is too long.") else: iban = (iban[4:] + iban[0:4]).upper() iban2 = '' for ch in iban: if ch.isdigit(): iban2 += ch else: iban2 += str(10 + ord(ch) - ord('A')) iban = int(iban2) if iban % 97 == 1: print("IBAN entered is valid.") else: print("IBAN entered is invalid.") |

