Python extract all substrings

How to Extract and Modify Substrings in Python?

The term substring refers to a sequence of characters that form a contiguous part of a string. For example, the string “Python Guide” contains the substring “Python.” Substrings can be of any length, from a single character to the entire string.

The purpose of this post is to provide an in-depth guide to Python substring by using numerous examples. The contents are as follows:

  • Extract Substrings
  • Modify Substrings
  • Check the Presence of a Substring in a String

How to Extract Substrings in Python?

To extract a substring from the string, various methods, such as string slicing, find(), index(), string indexing, etc., are used in Python. Here are some examples that will help you understand all of these methods:

Example 1: String Slicing Method in Python

Python offers many ways to extract substrings from a string. One of the ways is called “slicing”. We can use the slice notation [start:end] to extract a substring from an input string. Here is the syntax:

  • In the above syntax, the “start:” indicates the starting index of the substring. The character at this index is included in the substring. If the start is not included, it is assumed to be 0.
  • The parameter “end:” indicates the ending index of the particular substring. The character at this index is not part of the substring. If the end is not included, the string’s length is assumed.
  • A “step:” parameter specifies the number of characters to skip before taking the next one. The step is assumed to be one if it is not included.

Let’s understand this method with the following example code:

string_value = "Python Guide" substring = string_value[7:12] print(substring)
  • The string named “string_value” is initialized.
  • The “string slicing” method extracts the specified string from the given string.

The specified substring has been extracted.

Example 2: Using the find() Method

The “find()” method is utilized for finding the index of a specified substring in a given string. Here is the syntax for the method:

string.find(substring, start, end)

In the above syntax, “substring” is the substring we want to find the index, and “start” and “end” is the optional starting and ending index of the search. Let’s use this method to find the substring using the below example code:

string_value = "Python Guide" index_value = string_value.find("Guide") print(index_value) substring = string_value[index_value:index_value+5] print(substring)
  • The “find()” method takes the substring as an argument and finds the index of that specified substring in the given string.
  • The string-slicing approach uses the index position of the substring and returns the value of the substring from the string.
Читайте также:  Генерация случайных символов javascript

The index position and substring have been extracted from the string.

Example 3: Using the index() Method

The “index()” method finds the index of a particular substring within an input string. Here is an example code:

string_value = "Python Guide" index_value = string_value.index("Guide") print(index_value) substring = string_value[index_value:index_value+5] print(substring)
  • The “index()” method takes the substring as an argument and returns the index value of the substring from the input string.

The index position of the string has been extracted along with the substring.

Example 4: String Indexing in Python

The concept of string indexing is used in Python to access individual characters within a string based on their position or index number. Strings contain unique indexes for each character, ranging from 0 for the first character to n-1 for the nth, where n is the length.

Here is an example of positive string indexing in Python:

Code: (Using Positive Indexing)

string_value = "Python Guide" substring_value = string_value[7:12] print(substring_value)

The positive index of the specified substring is used to get that substring from the input string.

The specified substring has been obtained using the index value.

Negative indexing can also access characters at the end of a string. As an example, we can use index -1 to access the last character in the string:

Code: (Using Negative Indexing)

string_value = "Python Guide#" substring_value = string_value[-6:-1] print(substring_value)
  • The negative indexing is used to get the substring from the end side of the string.
  • The negative indexing slices the string from index -6 to index -1 (excluding -1).

The substring has been fetched using negative indexing.

How to Modify Substrings in Python?

A substring can also be modified by replacing, inserting, or deleting characters in addition to being extracted. Let’s modify the substring using these methods:

Example 1: Replace a Substring From the String

The following code uses the “replace()” function to replace the substring in the string:

string_value = "Python Guide" print('Original String', string_value) substring = string_value.replace("Python", "Java") print('After Replacement: ', substring)

The “string.replace()” function is used to replace the specified substring from the targeted substring in the given string.

The specified substring has been replaced by the given substring.

Example 2: Insert a Substring From the Input String

The below code is utilized to insert the substring from the given string:

string_value = "Hello " sub_string = "Python" index = 6 new_string_value = string_value[:index] + sub_string + string_value[index:] print(new_string_value)
  • The new string is created by taking the first 6 characters of the first string using the slicing method.
  • The new string is added to the second string (sub_string).
  • After that, the string containing the first 6 characters and the substring is added to the left characters of the first created string.

The new substring has been added to the given string.

Читайте также:  Java trees and graphs

Example 3: Removing Substring From the String

The below code is used to remove a substring from the string:

string_value = "Python Guide" substring = "Guide" output = string_value.replace(substring, "") print(output)
  • The string and substring are initialized.
  • The “string.replace()” function removes the substring “Guide” from the input string “Python Guide”.

The substring has been removed from the string.

How to Check the Presence of a Substring in a Given String?

To check the presence of a substring in a string, the “find()” method is used in Python. The “find()” method returns the index of the first appearance of a substring in the string. This function will retrieve -1 if the specified substring is not located/found. Here is an example code:

string_value = "Python Guide" substring = "Python" if string_value.find(substring) != -1: print("Substring is Present!") else: print("Substring is Not Present!")
  • The string and substring are initialized.
  • The “find()” method is used to check/verify if the substring is present/exists in the string.
  • If the index returned by the “find()” method is not “-1” then the substring is present in the string.

The specified substring is present in the string.

Conclusion

A substring is part of a string that is composed within another string. To manipulate strings, the substring operations, such as extracting parts of a string, checking if a string contains a certain substring, replacing, removing, inserting substrings with other strings, etc., are used in Python. To perform these operations, various functions such as replace(), string concatenation, string slicing, find(), etc., are used in Python. The guide explained Python substrings in-depth with examples to illustrate the topic.

Источник

Extract Substring From a String in Python

Extract Substring From a String in Python

  1. Extract Substring Using String Slicing in Python
  2. Extract Substring Using the slice() Constructor in Python
  3. Extract Substring Using Regular Expression in Python

The string is a sequence of characters. We deal with strings all the time, no matter if we are doing software development or competitive programming. Sometimes, while writing programs, we have to access sub-parts of a string. These sub-parts are more commonly known as substrings. A substring is a subset of a string.

In Python, we can easily do this task using string slicing or using regular expression or regex.

Extract Substring Using String Slicing in Python

There are a few ways to do string slicing in Python. Indexing is the most basic and the most commonly used method. Refer to the following code.

myString = "Mississippi" print(myString[:]) # Line 1 print(myString[4 : ]) # Line 2 print(myString[ : 8]) # Line 3 print(myString[2 : 7]) # Line 4 print(myString[4 : -1]) # Line 5 print(myString[-6 : -1]) # Line 6 
Mississippi issippi Mississi ssiss issipp ssipp 

In the above code, we add [] brackets at the end of the variable storing the string. We use this notation for indexing. Inside these brackets, we add some integer values that represent indexes.

This is the format for the brackets [start : stop : step] (seperated by colons ( : )).

By default, the value of start is 0 or the first index, the value of stop is the last index, and the value of step is 1 . start represents the starting index of the substring, stop represents the ending index of the substring, and step represents the value to use for incrementing after each index.

Читайте также:  Программы для микроконтроллеров java

The substring returned is actually between start index and stop — 1 index because the indexing starts from 0 in Python. So, if we wish to retrieve Miss from Mississippi , we should use [0 : 4]

  • [:] -> Returns the whole string.
  • [4 : ] -> Returns a substring starting from index 4 till the last index.
  • [ : 8] -> Returns a substring starting from index 0 till index 7 .
  • [2 : 7] -> Returns a substring starting from index 2 till index 6 .
  • [4 : -1] -> Returns a substring starting from index 4 till second last index. -1 can be used to define the last index in Python.
  • [-6 : -1] -> Returns a substring starting from the sixth index from the end till the second last index.

Extract Substring Using the slice() Constructor in Python

Instead of mentioning the indexes inside the brackets, we can use the slice() constructor to create a slice object to slice a string or any other sequence such as a list or tuple.

The slice(start, stop, step) constructor accepts three parameters, namely, start , stop , and step . They mean exactly the same as explained above.

The working of slice is a bit different as compared to brackets notation. The slice object is put inside the string variable brackets like this myString[] .

If a single integer value, say x , is provided to the slice() constructor and is further used for index slicing, a substring starting from index 0 till index x — 1 will be retrieved. Refer to the following code.

myString = "Mississippi" slice1 = slice(3) slice2 = slice(4) slice3 = slice(0, 8) slice4 = slice(2, 7) slice5 = slice(4, -1) slice6 = slice(-6, -1) print(myString[slice1]) print(myString[slice2]) print(myString[slice3]) print(myString[slice4]) print(myString[slice5]) print(myString[slice6]) 
Mis Miss Mississi ssiss issipp ssipp 

The outputs received are self-explanatory. The indexes follow the same rules as defined for brackets notation.

Extract Substring Using Regular Expression in Python

For regular expression, we’ll use Python’s in-built package re .

import re  string = "123AAAMississippiZZZ123"  try:  found = re.search('AAA(.+?)ZZZ', string).group(1)  print(found) except AttributeError:  pass 

In the above code, the search() function searches for the first location of the pattern provided as an argument in the passed string. It returns a Match object. A Match object has many attributes which define the output such as the span of the substring or the starting and the ending indexes of the substring.

print(dir(re.search(‘AAA(.+?)ZZZ’, string))) will output all the attributes of the Match object. Note that some attributes might be missing because when dir() is used, __dir__() method is called, and this method returns a list of all the attributes. And this method is editable or overridable.

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article — Python String

Источник

Оцените статью