- What is a Substring in Python?
- String Slicing in Python
- More Examples on Python Sub Strings
- 1. Slice a Python String Without Using the End Index String[StartIndex:]
- 2) Slice a Python String Without Specifying the Starting Index String[:EndIndex]
- 3. Slice a Python String Without Specifying the Starting and Ending Index Values string[:]
- 4. String Slicing With Steps
- 5. Slice a Python Sting with Step Count 2
- 6. Reverse a String Using Slicing in Python
- Find All The Possible Substrings of a String in Python
- Find All the Possible Substrings of a String in Python using List Comprehension
- How to Check If a String is a Substring?
- Summary
In Python, the string is a sequence of Unicode characters, represented within the single or double quotes. A string can contain any character, including alphanumeric and special symbols. In this tutorial, we will learn how to extract a substring from a string in Python. Also, we will know the different methods that we can use to extract a substring from a string.
What is a Substring in Python?
A substring is a sequential part of a string. If a string a is present in a string b , then string a would be considered as a sub-string of b. Also, in Python, this substring is also referred to as a Python string slicing .
String Slicing in Python
In Python, to access a string, we use the variable name of the string. By only using the variable name, we can get the complete string, but if we want to retrieve a part or a specific character from the string, then we use indexing and slicing. In slicing, we use index values of a given string to grab a sequence of characters from the string.
Note : The index value starts from 0.
Code Example
Output
More Examples on Python Sub Strings
1. Slice a Python String Without Using the End Index String[StartIndex:]
If we do not specify the end index value, then it would be treated as the last index value. Code Example
Output
2) Slice a Python String Without Specifying the Starting Index String[:EndIndex]
If we do not specify the starting index while slicing the string, then the starting index becomes 0 by default.
Code Example
Output
3. Slice a Python String Without Specifying the Starting and Ending Index Values string[:]
If we do not specify the starting and ending index values for the slicing, then the slicing operation returns the complete string from index value 0 to (n-1), where n is the length of the string.
Code Example
Output
4. String Slicing With Steps
By far, we only discussed how we could specify the starting and ending index values during slicing to get a substring. But slicing also supports step value, which describes in what sequence the substring should be retrieved from a string, and the step value is the last part of the slicing.
Syntax
< Note >: By default, the step value is 1, which signifies that the step count should be +1 from the starting index to the end index. And its value could be any integer number except 0.
5. Slice a Python Sting with Step Count 2
If the step value is 2, then the string slice will skip every consecutive value and grab the alternative one.
Code Example
Output
6. Reverse a String Using Slicing in Python
A string can be reversed using slicing in Python . By setting the step value to -1 and leaving the startIndex and endIndex blank, the string will be reversed. The -1 represents that the count should be negative and 1 step at a time.
Code Example
Find All The Possible Substrings of a String in Python
To find all the possible substrings of a string in Python, we have to create a nested for loop.
Code Example
Output
< Note >: An empty string is also a substring.
Find All the Possible Substrings of a String in Python using List Comprehension
Python list comprehension provides an alternative to creating a list of elements using for loop within a single line. Here is an example of finding all the possible substrings of a string using list comprehension. Code Example
Output
How to Check If a String is a Substring?
To check if a string is a substring of another string, we can use the
in
operator. The Python operator checks if the string is a substring and returns True. Else, it returns False.
Code Example
Summary
In this tutorial, you learned all about how to substring a string in python. You also learned how to find all the possible substrings for a string and how to check if a string is a substring or not using the in operator. A substring in Python is also known as string slicing, and most of the time, you will be hearing and using the word slicing.
People are also reading:
- Edge Detection in Python
- Python HOG Feature Extraction
- Automated Browser Testing in Python
- Python Assembly, Disassembly and Emulation
- Python Main Function & Method
- Reading and Writing CSV Files in Python
- Python map() function
- Starting Python Coding on a MacBook
- Python Extract all stored Chrome Password
- Gmail API in Python
Leave a Comment on this Post