Strings in Python

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:

or

 

2. The length of a string is determined by the len() function. The escape character (\) is not counted. For example:

2

2

 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:

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:

 

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.

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;

Abcd
Alpha
Alpha
alpha
123
Αβγδ

  • center() – centers the string inside the field of a known length;

[   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:

[*******gamma********]

 

  • count() – counts the occurrences of a given character;

  • join() – joins all items of a tuple/list into one string;

omicron,pi,rho

John#Peter#Vicky

 

lower() – converts all the string’s letters into lower-case letters;

sigma=60

 

lstrip() – removes the white characters from the beginning of the string;

[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:

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.

[ 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.

[aleph]

of all fruits banana is my favorite

banana

 

split() – splits the string into a substring using a given delimiter;

['phi', 'chi', 'psi']

['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;

www.pythoninstitute.org
Thare are it!
Apple

The three-parameter replace() variant uses the third argument (a number) to limit the number of replacements.

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
-1

 

rfind() – finds a substring starting from the end of the string; string.rfind(value, start, end) 

8
-1
4

12

8

 

swapcase() – swaps the letters’ cases (lower to upper and vice versa)

i KNOW THAT i KNOW NOTHING.

 

title() – makes the first letter in each word upper-case;

I Know That I Know Nothing. Part 1.

 

upper() – converts all the string’s letter into upper-case letters.

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?

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.

False
True

 

isalnum() – does the string consist only of letters and digits?

True
True
True
False
False
False

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?

True
False

 

isdigit() – looks at digits only – anything else produces False as the result.

True
False

 

islower() – does the string consists only of lower-case letters?

False
True

isupper() – does the string consists only of upper-case letters?

False
False
True

isspace() – does the string consists only of white spaces?

True
True
False

 

 

Exercise 1

What is the length of the following string assuming there is no whitespaces between the quotes?

 

1

Exercise 2

What is the expected output of the following code?

 

['t', 'e', 'r']

Exercise 3

What is the expected output of the following code?

 

bcd

Exercise 4

What is the expected output of the following code?

 

ABC123xyx

Exercise 5

What is the expected output of the following code?

 

of

Exercise 6

What is the expected output of the following code?

 

Where*are*the*snows?

Exercise 7

What is the expected output of the following code?

 

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 == number is always False;
    • string != number is always True;
    • string >= number always raises an TypeError exception.

True

String comparison is always case-sensitive (upper-case letters are taken as lesser than lower-case).

True

Let’s check it:

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;

['omega', 'alpha', 'pi', 'gamma']

['alpha', 'gamma', 'omega', 'pi']

 

    • a method named sort(), which sorts the list in place

['omega', 'alpha', 'pi', 'gamma']

['alpha', 'gamma', 'omega', 'pi']

 

3. A number can be converted to a string using the str() function.

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).

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?

are

['Where', 'are', 'of', 'snows', 'the', 'yesteryear?']

 

Exercise 3

What is the expected result of the following code?

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.

 

The Caesar Cipher: decrypting a message

 

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.

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.