How to Substring a String in Python
Last Updated :
09 Aug, 2024
A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a string in Python.
Substring a string in Python
- Using String Slicing
- Using str.split() function
- Using Regular Expressions
Using String Slicing to get the Substring
Consider the string " GeeksForGeeks is best! ". Let's perform various string-slicing operations to extract various substrings
Extracting a Substring from the Beginning
In this example, we are trying to extract the starting word from the string.we used string slicing to extract the substring "Geeks" from the beginning of the string. The slice notation str[:5] starts at index 0 (the first character) and goes up to, but does not include, index 5, resulting in "Geeks".
Python
# code
str = "GeeksForGeeks is best!"
substring_start = str[:5]
print(substring_start)
Output
Geeks
Extracting the Last Portion of the String
In this example we are trying to extract the last portion of the string.we used string slicing to extract the substring "best!". By omitting the end index, the slice extends to the end of the string, resulting in "best!".
Python
# code
str = "GeeksForGeeks is best!"
substring_last = str[17:]
print(substring_last)
Output
best!
Extracting a Substring from the Middle
In this example we are trying to extract the middle portion of the string.In this example, we specified both the start and end indices to extract the substring "is" from the text. The slice notation text[14:16] starts at index 14 and goes up to, but does not include, index 16, resulting in "is".
Python
# code
str = "GeeksForGeeks is best!"
substring = str[14:16]
print(substring)
Output
is
Using str.split() function
We can use the split() function to get the substrings.The split() method effectively splits the string "Geeks For Geeks" into words based on spaces. So, the resulting substrings list contains each word as an element
Python
# code
str="Geeks For Geeks"
substrings=str.split()
print(substrings)
Output
['Geeks', 'For', 'Geeks']
Using Regular Expressions
We can use re.findall() method to find all the substrings with the regular expressions.we have used the regular expression 'w+' which matches one or more word characters. We then used re.findall() function to get all the strings based on the regular expression specified. The resulting output is individual words as substrings.
Python
import re
str = "GeeksforGeeks is best!"
pattern = r'\w+'
substrings = re.findall(pattern, str)
print(substrings)
Output
['GeeksforGeeks', 'is', 'best']
Similar Reads
How to reverse a String in Python Reversing a string is a common task in Python, which can be done by several methods. In this article, we discuss different approaches to reversing a string. One of the simplest and most efficient ways is by using slicing. Letâs see how it works:Using string slicingThis slicing method is one of the s
4 min read
How to Index and Slice Strings in Python? In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.Table of ContentIndexing Strings in PythonAccessing
2 min read
String Subsequence and Substring in Python Subsequence and Substring both are parts of the given String with some differences between them. Both of them are made using the characters in the given String only. The difference between them is that the Substring is the contiguous part of the string and the Subsequence is the non-contiguous part
5 min read
Check if String Contains Substring in Python This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx
8 min read
Python - Check if substring present in string The task is to check if a specific substring is present within a larger string. Python offers several methods to perform this check, from simple string methods to more advanced techniques. In this article, we'll explore these different methods to efficiently perform this check.Using in operatorThis
2 min read
String Slicing in Python String slicing in Python is a way to get specific parts of a string by using start, end and step values. Itâs especially useful for text manipulation and data parsing.Letâs take a quick example of string slicing:Pythons = "Hello, Python!" print(s[0:5])OutputHello Explanation: In this example, we use
4 min read
replace() in Python to replace a substring replace() method in Python allows us to replace a specific part of a string with a new value. It returns a new string with the change without modifying the original one. We can also specify how many times the replacement should occur.For Example:Pythons = "hlo AB world" # Replace "AB" with "C" in `s
1 min read
How to Remove Letters From a String in Python Removing letters or specific characters from a string in Python can be done in several ways. But Python strings are immutable, so removal operations cannot be performed in-place. Instead, they require creating a new string, which uses additional memory. Letâs start with a simple method to remove a s
3 min read
Python String rfind() Method Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1.ExamplePythons = "GeeksForGeeks" print(s.rfind("Geeks"))Output8 Explanationstring "GeeksForGeeks" contains the substring "Geeks" twice.rfind() method starts the sea
4 min read
How to Change Values in a String in Python The task of changing values in a string in Python involves modifying specific parts of the string based on certain conditions. Since strings in Python are immutable, any modification requires creating a new string with the desired changes. For example, if we have a string like "Hello, World!", we mi
2 min read