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.

pasword = input("Enter your password:")
if len(password)<8:
   print("Your password is not long enough!")

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]

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
firstCharacter = alphabet[0]
lastCharacter = alphabet[25]

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:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print("The first 5 letters of the alphabet are: " + alphabet[:5])
print("The last 5 letters of the alphabet are: " + alphabet[-5:])
print("The letters in between are: "  + alphabet[5:-5])

Slicing a date:

date = "05 January 2019"
day = date[:2]  #First 2 characters
year = date[-4:] #Last 4 characters
month = date[3:-5] #The remaining characters in the middle

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.

dateOfBirth = "15 January 2000"
if "January" in  dateOfBirth:
   print("You were born in January!")

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

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
pos = alphabet.find("C")
print("Letter C is at position: " + str(pos))

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:

firstname = "James"
print("Hello " + firstname)

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 lastname.
    Finally the program should display whether the user is a Student, a Teacher or an Admin member of staff.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3 / 5. Vote count: 88

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: