String Slicing in Python

String?


In computer science, a string is a piece of text or a collection of characters.
string-manipulation-1

String Length?


The length of a string represents the number of characters it contains. In pyhon you can use the function len() to find out the length of a string.

String Slicing?


string-sliceOn occasions, you will want your program to extract a few characters from a string. This is called string slicing.

You may for instance extract the first 5 characters of a phone number to retrieve the area code. Or you may need to extract the last 4 characters of a date to extract the year part.

Extracting a single character at a given position:

You can retrieve a character at a given position as follows:

string[position]

Note: The first character is always at position 0!

Extracting a substring (multiples characters) from a string:

To slice a string in python you have to think about the starting position and the ending position and use the following syntax:

string[start:end]

This syntax will extract all the characters from position start up to but not including position end.

If start is left empty, the substring will start from position 0.
If end is left empty, the substring will end at the last character of the string.

A negative start value or end value is used to identify a position (number of characters) from the end of the string. This can be useful to extract the last few characters of a string.

Try it yourself
Enter a string below and use the two handles to change the start and end position and slice your string accordingly. The resulting substring will appear in purple.

Slicing the alphabet:

Slicing a date:

Locating if a substring is in a string (and retrieving its position)?


You can easily find if a substring is in a string using the keyword in.

You can also retrieve the exact position of a substring in a string using the find() function:

This script would tell us that letter “C” is at position 2. (Even though it is the third letter of the alphabet). That’s because the first letter in a string is always at position 0, not 1!

Note that if the substring is not found in the string, the find() function will return the value -1.

String Concatenation?


The act of joining two strings together is called string concatenation.
string-Concatenation

In Python string concatenation is done using the + sign:

Python Code:


Python Challenge


Your challenge is write a program to ask the user to enter their school username in the following format:

  • Year Group Using two digits (e.g. “07” for year 7, “11” for year 11, “00” for staff members)
  • 1 character for their initial (first letter of their firstname)
  • The user’s lastname
  • A 2-digit code: “_S” for students, “_T” for teachers, “_A” for admin staff.

For instance the following usernames are valid usernames:


07jFox_S
09kJohnson_S
11rTaylor_S
00pJones_T
00jOliver_A

Your program should read the username and decide:

  • If the username is less than 6 characters long the program should ask the user to enter a valid username.
  • If the username does not contain the character “_” it should also ask the user to enter a valid username.
  • If the username is valid, the program should decide if the user is a member of staff or a student.
  • If they are a student the programme should find out their year group.
  • The program should also display the initial of the student as well as their lastame.
  • Finally the program should display whether the user is a Student, a Teacher or an Admin member of staff.
Share Button
Tagged with: