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][1] course.
* Indexing and slicing strings: Watch the “String Indexing and Slicing” video from the [Certified Entry-Level Python Programmer Certification][1] course.
* Printing to the screen: Watch the videos from the “Input and Output Operations” section of the [Certified Entry-Level Python Programmer Certification][1] course.
[1]: https://linuxacademy.com/cp/modules/view/id/413
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create the string-info.py Script and Take User Input
We want to be able to execute our script, so to begin let’s create an empty script with a shebang and make it executable:
$ echo '#!/usr/bin/env python3.7' > string-info.py $ chmod +x string-info.py
Our goal is to have the script take in some user input and then print a handful of lines to the screen. Let’s take the user input now.
string-info.py
#!/usr/bin/env python3.7 message = input("Enter a message: ")
- Print the First, Last, and Middle Characters from the User’s Input
To print the first, last, and middle characters from a string, we need to index the message. The first and last characters will be straight forward so let’s add those initially:
string-info.py
#!/usr/bin/env python3.7 message = input("Enter a message: ") print("First character:", message[0]) print("Last character:", message[-1])
Getting the middle character is a little more complicated. It will have the same number of characters before and after it. There is a character in the middle of a string if it has an odd length (eg:
abc
hasb
in the middle), but for an even length string, then the middle is really between two indexes (eg:abcd
where the middle is betweenbc
).We can calculate the middle by dividing the length of the string by 2 and truncating it to get the index value. We can truncate the value by casting the result of the division back to being an integer. Here’s what our print function call looks like:
string-info.py
#!/usr/bin/env python3.7 message = input("Enter a message: ") print("First character:", message[0]) print("Last character:", message[-1]) print("Middle character:", message[int(len(message) / 2)])
- Print the Even Index Characters and Odd Index Characters
To print the even index characters and the odd index characters, we’ll need to use slicing with the step option. The only thing that we need to change between these two lines will be the starting index, as long as we step by
2
. Here’s what it these lines look like:string-info.py
#!/usr/bin/env python3.7 message = input("Enter a message: ") print("First character:", message[0]) print("Last character:", message[-1]) print("Middle character:", message[int(len(message) / 2)]) print("Even index characters:", message[0::2]) print("Odd index characters:", message[1::2])
- Print the String in Reverse
Reversing a string can be done by getting a little creative with how slicing works. If we don’t provide a start or end index, and then set a
-1
step value, then we’ll reverse the string.string-info.py
#!/usr/bin/env python3.7 message = input("Enter a message: ") print("First character:", message[0]) print("Last character:", message[-1]) print("Middle character:", message[int(len(message) / 2)]) print("Even index characters:", message[0::2]) print("Odd index characters:", message[1::2]) print("Reversed message:", message[::-1])