Indexing and Slicing Python Strings

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

Accessing characters, whether they are single or multiple, is an essential skill for working with strings in any programming language. In Python, we do this by indexing and slicing the string. In this hands-on lab, we’ll go through writing some code to demonstrate that we understand how to use indexing and slicing by printing out different pieces of information about a string back to the user.

To feel comfortable completing this lab, you’ll want to be familiar with the following:

* Working with string literals: Watch the “Strings and String Operators” video from the Certified Entry-Level Python Programmer Certification course.
* Indexing and slicing strings: Watch the “String Indexing and Slicing” video from the Certified Entry-Level Python Programmer Certification course.
* Printing to the screen: Watch the videos from the “Input and Output Operations” section of the Certified Entry-Level Python Programmer Certification course.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the string-info.py Script and Take User Input
  1. We want to be able to execute our script, so to begin create an empty script named string-info.py with a shebang and make it executable:

    $ echo '#!/usr/bin/env python3.7' > string-info.py
    $ chmod +x string-info.py
  2. Create a script that asks for user input, store it as the message variable, and print a handful of lines to the screen. Let’s take the user input now.

Print the First, Last, and Middle Characters from the User’s Input
  1. Print the first character from your string.
  2. Print the last character from your string.
  3. Print the middle characters from a string.
Print the Even Index Characters and Odd Index Characters
  1. Print the even index characters of your string.
  2. Print the odd index characters of your string.
Print the String in Reverse
  1. Reverse the order of your string and print it.

Additional Resources

We're going to write a script that takes user input, so that we're not working with static content, and print out some information and permutations of the string that the user has entered. Our string-info.py script will print the following information about the user provided message:

  • The first character
  • The last character
  • The middle character (for even length strings we'll return the integer just after the exact center).
  • Every even index character
  • Every odd index character
  • The message in reverse

Here's our script in action:

$ python3.7 string-info.py
Enter a message: Test Message
First character: T
Last character: e
Middle character: e
Even index characters: Ts esg
Odd index characters: etMsae
Reversed message: egasseM tseT

Logging In

Using the Terminal To Complete the Lab

There are a couple of ways to get in and work with the code. One is to use the credentials provided in the lab, log in with SSH, and use a text editor in the terminal, such as Vim.

Note: When copying and pasting code into Vim from the lab guide, first enter :set paste (and then i to enter insert mode) to avoid adding unnecessary spaces and hashes. To save and quit the file, press Escape followed by :wq. To exit the file without saving, press Escape followed by :q!.

Using VS Code To Complete the Lab

You can also access the lab using VS Code in the browser. If you'd like to go this route, then follow the steps below:

  1. Navigate to the public IP address of the workstation server (provided in your lab credentials) on port 8080, using http (e.g., http://PUBLIC_IP:8080).
  2. If you receive a notification indicating the connection is not secure, click Advanced. Then, proceed to the server.
  3. Use the password provided in your lab credentials.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?