Python 9a - String Handling

What is String Handling?

String handling refers to the manipulation of a string variable, typical uses include:

  • Checking the length of a variable.

  • Searching a variable for a certain phrase.

  • Checking the number of times a specific character or word is used.

In the real-world, string handling is used to examine passwords and to ensure that they are of an acceptable strength (by checking that they include a mixture of capital letters, lowercase letters and symbols for example).

Counting in a String

The easiest way to count how many times a certain value appears within a variable is to use the .count() command.

 

It is important to note that, just like when using an input statement or calculation line, you must save the calculation into a variable.

 

An example, for counting the number of e’s in a sentence, is below:

string5.png

Practice Task 1

Create a program that counts how many instances of the letter a have been entered in a sentence.

Example solution:

py186.PNG

Finding the Length of a String

Just like when we wanted to find the length of a list, we use the len command to see how many characters are in a string.

 

We also need to save the calculation into a variable.

A common reason for finding the length is as part of validation, for example, checking a password is more than 8 characters we would code:

string8.png

Practice Task 2

Create a program that asks for a name.

Check that the name is between 4 and 10 characters.

Print appropriate messages if it is within this range and if it isn't.

Example solution:

py187.PNG

Checking the Start / End of a String

To determine if the first character in a string is a specific value use the .startswith() command.

 

Remember to save the calculation into a variable. For example:

start = sentence.startswith(“A”)

If the sentence above does start with the letter A then the Boolean value True will be saved as start, else if the variable begins with any other character, then start will be saved as False instead.

 

If we were creating a program for a library and we wanted to check if a book reference began with “Geo” then we would use the following code:

string10.png

Above we have used an if statement to see if the startswith value is True or False and printed an appropriate comment for either scenario.

 

Careful not to put the True value in speech marks as it won’t work.

 

If we ran the above code, two different outcomes could be:

Similarly, you can use .endswith() to check the last characters of a string.

Practice Task 3

Ask a user to enter their name.

If their name starts with the same letter as your name then print an appropriate message.

Else, print a different message.

Example solution:

Reversing a String

To reverse a string, you write the variable name and then use square brackets to move one character at a time backwards.

 

The first two colons are left empty as they denote where to start and end from (which don’t need to be changed).

Therefore the -1 states that it will reverse from the end to the start:

string14.png

Ask the user to enter a random sentence.

Print the sentence in reverse.

Example solution:

py189.PNG

Practice Task 4

Printing Parts of a String

You may want to print just part of a string or variable using square brackets.

 

You can also use len to work out the length and work back, if you want to display the last characters:

string16.png

Practice Task 5

Ask the user to input a long word.

Output the middle character of the word.

Example solution:

py190.PNG

Split Strings

Use the .split command to split a string into separate words.

An empty split command such as words.split() will split at each space.

split1.png
split2.png

You can enter a value in the brackets to split at certain characters, such as words.split(",")

split5.png
split6.png

Use the len function to count the number of words once they have been split.

spit3.png
split4.png

You can use a for loop to cycle through each word. The program below checks the length of each word.

split7.png
split8.png

Practice Task 6

Ask the user to input a sentence.

Calculate and print the amount of words in the sentence.

Calculate and print the amount of words that begin with s.

Example solution:

split9.png