15th Apr 2024 7 minutes read String Slicing in Python: A Complete Guide Soner Yıldırım python learn python What is a string slice in Python? This article will explain several examples to show how to slice a string in Python. It also has an exercise for you to solve. Data does not always come in numbers. Textual data constitutes a big portion of data workflows. What differentiates textual data from numerical data is that it usually requires much more data cleaning and processing. We can take a numerical value as-is, but it’s quite often different with text. By textual data, we don’t just mean natural language text; it includes any sequence of alphanumeric or other characters. This form of textual data is known as a string. We offer a great course on Working with Strings in Python, which will provide you with extensive knowledge of Python string operations. A string in Python can contain letters, numbers, spaces, punctuation, or any other characters. There are many different operations we can do with strings. For instance, we can combine multiple strings into one string. If you’d like to learn more about this operation, read the article How to Append a String in Python. In some cases, strings contain multiple pieces of information – or some redundant parts that need to be removed. One way of doing such operations is slicing, which basically means to obtain a substring from a string. In this article, we’ll learn every aspect of slicing a string in Python. Index Values in Python Strings Strings are a sequence of characters; slicing allows us to extract a partial sequence from a string. To slice a string in Python, we use character indexes. The index of the first character in a string is 0, the second character is 1, and so on. To extract a character from a string, we can write its index in square brackets. Here is an example: >>> name = "John" >>> name[0] Result: 'J' Let’s try another example: >>> name[1] Result: 'o' We can also start the index from the end of the string. In that case, the index starts from -1. The index of the last character is -1, the second from last is -2, and so on. Let’s do an example using negative index values: >>> name = "John" >>> name[-1] Result: 'n' And another: >>> name[-2] Result: 'h' Slicing a String In Python We’ve learned the index values of characters in a string and how to use them to get a part of the string. To select a multi-character substring, we simply specify the index of the starting and ending characters of the slice we want to extract. The starting and ending index values are separated by a colon. Example 1: Basic Slicing Syntax Let’s say we have a string that contains both letters and numbers. We need to extract the first three characters from this string and save it as a new variable. Here’s how we do it: >>> product_id = "ADB324" >>> sub_id = product_id[0:3] >>> print(sub_id) Result: ADB The expression [0:3] creates a slice that contains the first, second, and third characters. Important: When specifying a slice, the upper bound is not inclusive. You must add 1 to your desired ending index. That’s why expression [0:3] gives us the characters with index values 0, 1, and 2. If the string slice starts from the beginning, we don’t have to write the starting index. Thus, [0:3] and [:3] extract the same substring. Example 2: Slicing from the Middle Let’s extract a slice that does not start from the beginning. >>> description = "?long-sleeve" >>> print(description[1:5]) Result: long The expression [1:5] creates a slice from the second character up to the fifth one. Just as we don’t need to write the starting index if the slice starts from the beginning of a string, we don’t need to specify the ending index if we want the slice to cover all characters until the end: >>> description = "?long-sleeve" >>> print(description[1:]) Result: long-sleeve In this example, we create the slice using [1:], which includes all the characters from the second one up to the end of the string. Example 3: Slicing with Negative Index Values We can also use negative index values to slice a string in Python. Let’s say we need to extract a slice to include the last 6 characters. We just need to specify the starting index as -6; we can omit the ending index. >>> description = "?long-sleeve" >>> print(description[-6:]) Result: sleeve Using Step in String Slicing Python provides more flexible ways to slice a string. By default, string slice includes all the characters between the given starting and ending point. We can change this behavior using the step size parameter. The default value of the step size is 1, which selects every character. If we change it to 2, the string slice contains every other character within the defined sequence. Example 4: Slicing with Step Size The step size comes after the ending index and is separated from it by another colon. Thus, the full syntax of string slicing in Python is as follows: string[start : stop : step] If we want to use the default value for the step size, we leave it blank. Let’s do an example to create a slice that contains every other character in a string: >>> mystring = "python is awesome!" >>> print(mystring[::2]) Result: pto saeoe In this example, we want to start from the beginning and go until the end, so we omit the starting and ending indexes. To select every other character, the step size should be 2. Thus, the expression [::2] selects the first character (which is p). It skips the second character and selects the third one (t). This logic continues until we reach the end. Using the Python slice() Function Another way of slicing a string in Python is the slice() function. It returns a slice object representing the set of indices specified by start, stop, and step parameter. Slice objects contain a portion of a sequence. This is best explained via an example. Example 5: Basic Syntax for the slice() Function >>> mystring = "Jennifer" >>> x = slice(1, 4) >>> print(mystring[x]) Result: enn The slice(1, 4) selects second, third, and fourth characters. The default step size is 1. Let’s do another example with a step value other than 1. Example 6: slice() with Step >>> mystring = "python is awesome!" >>> x = slice(0, 10, 2) >>> print(mystring[x]) Result: pto s In this example, we first create a slice object that contains every other character between the first and tenth characters. The upper bound is exclusive, so it does not include the tenth character. The equivalent of this slice() command in bracket notation is [0:10:2]. >>> mystring = "python is awesome!" >>> print(mystring[0:10:2]) Result: pto s Use Cases for String Slicing Let’s go over a couple of examples to demonstrate how string slicing can be used for cleaning and processing raw data. Use Case 1: Cleaning Price Values as Strings Consider a case where price values are stored as strings (including the currency sign). We should convert it to a numerical data type, but we first need to remove the currency sign, which is the first character. We can complete this task by simply slicing price strings, starting from the second character. >>> price_string = ""$120.11"" >>> price = float(price_string[1:]) >>> print(price) Result: 120.11 Exercise: Extracting Size Codes from Product Codes The second one is for you to solve. Here’s the exercise. We have an apparel store. Each product has an item code that contains the product ID and the size code; the last three characters in the item code are the size code. Here’s the item code: >>> item_code = "TX12400X00" Your task: Extract the size code using string slicing. Solution: The size code in the given item code above is X00. Please try to extract this size code using Python string slicing methods. Here is the solution for this exercise: >>> size_code = item_code[-3:] >>> print(size_code) Result: X00 More About String Slicing in Python In this article, we learned how to take a slice from a string in Python. There are basically two ways of doing this. The first one is bracket notation, which defines a slice using the start, stop, and step values inside square brackets. The second way is the slice() function. It’s basically the same thing as the bracket notation, but we use the slice() function to create a slice object. The parameters are the same as for bracket notation. When working with strings, we need to be able to do much more than just slice out substrings. Cleaning messy raw textual data requires many different operations. Thankfully, Python provides several methods to clean, process, and manipulate strings. If you’d like to learn more about Python string operations, read the article An Overview of Python String Methods. If you want to learn more about strings and how to handle them in Python, I encourage you to take our course on Working with Strings in Python, which provides you with 57 interactive exercises so you can take your knowledge of strings to the next level. Tags: python learn python