Get String Between Two Characters in Python

Using the string-slicing technique

To get String between two characters in Python:

  • Use String’s find() method to find indices of both the characters.
  • Use String’s slicing to get String between indices of the two characters.

Output:

ava2Blo

In the above example, we found the position of characters J and g using the find() function and used the string-slicing technique to extract the string between these characters.

String’s find() method returns index of specified value and returns -1 if value is not present.

We used String slicing to find substring between indices of the two character.

String slicing refers to the technique of extracting parts of strings using the character’s index. We specify the start, end, and step values for string slicing within the square brackets. The step value specifies the increment between two successive values and is set to one by default.

Since start index is included while doing string-slicing, we used +1 for first character e.g. s.find('ch1')+1 as we didn’t want to include first character in the substring.

Using Regular Expressions

Use re’s search() method to get substring between two characters using regular expression, e.g. re.search(ch1+'(.+?)'+ch2, str).

We used re.search() method to get substring between two characters.

re.search() searches the String for a match and returns re.match object. If no matches found, it will return None.

The first argument we passed is regular expression and second argument is String in which you want to search on the based of regular expression.

We extracted matched String from re.match object m using m.group(1).

Here is pictorial representation of the regular expression:

Get String between two characters using regular expressions

m.group(0) returns fully matched String while m.group(1) returns first captured group in input String.

Using the split() function

  • Use split() to split the String based on first character.
  • From the extracted two parts, use the second part and split it again using the second character.
  • From the returned parts, the first one is the required string between the two characters.

Output:

ava2Blo

The split() function in Python can be used to split a string into two parts at the position of a specific character.

We used split() method to split the String Java2blog in two parts based on first character i.e. J. It returned list with two elements.i.e J and ava2blog.

We then again used split() method to split second part of the list ava2blog based on second character g. It returned list with two elements. ava2Blo and g.

First element of the concluded list is the substring between two characters J and g in String Java2blog

Conclusion

To conclude, we discussed several methods in this article to get string between two characters in Python.

In the first method, we used the string-slicing technique. In this method, we find the positions of the two characters and extract the required string between them using this technique.

The next method discusses the use of the re library for the same. This method uses regular expressions to find the required substring using a regex pattern with the re.search() function. In the final method, we used the split() function consecutively to split and get the string between two characters in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *