Use slicing, like [:3]
:
astr = 'accaggcatgattgcccgattccatgcggtcag'
x = astr.split('cat', 1)[-1][: 3]
print(x)
y = astr.split('cat', 2)[-1][: 3]
print(y)
Output:
gat gcg
Also, another idea could be:
print(list(map(lambda x: x[: 3], astr.split('cat')[1: ])))
You can also get all of them in one go:
[x[: 3]
for x in astr.split('cat')[1: ]
]
Output:
['gat', 'gcg']
Last Updated : 22 Jul, 2022
The original string: GeeksforGeeks is best
for geeks
The split string: best
String after the substring occurrence: for geeks
The original string: GeeksforGeeks is best
for geeks
The split string: best
String after the substring occurrence: for geeks
To split a text string at a specific character with a formula, you can use a combination of the LEFT, RIGHT, LEN, and FIND functions. In the example shown, the formula in C5 is:,In this example, the goal is to split a text string at the underscore("_") character with a formula. Notice the location of the underscore is different in each row. This means the formula needs to locate the position of the underscore character first before any text is extracted. ,Working from the inside out, this formula uses the FIND function to locate the underscore character ("_") in the text, then subtracts 1 to move back one character:,The Excel LEN function returns the length of a given text string as the number of characters. LEN will also count characters in numbers, but number formatting is not included.
= LEFT(text, FIND(character, text) - 1)
To split a text string at a specific character with a formula, you can use a combination of the LEFT, RIGHT, LEN, and FIND functions. In the example shown, the formula in C5 is:
= LEFT(B5, FIND("_", B5) - 1)
= LEFT(B5, FIND("_", B5) - 1)
And the formula in D5 is:
= RIGHT(B5, LEN(B5) - FIND("_", B5))
= LEFT(B5, FIND("_", B5) - 1)
= RIGHT(B5, LEN(B5) - FIND("_", B5))
To extract the text on the left side of the underscore, the formula in C5 is:
LEFT(B5, FIND("_", B5) - 1) // left
LEFT(B5, FIND("_", B5) - 1) // left
Working from the inside out, this formula uses the FIND function to locate the underscore character ("_") in the text, then subtracts 1 to move back one character:
FIND("_", B5) - 1 // returns 10
FIND returns 11, so the result after subtracting 1 is 10. This result is fed into the LEFT function as the num_chars argument, the number of characters to extract from B5, starting from the first character on the left:
= LEFT(B5, 10) // returns "Assessment"
= LEFT(B5, 10) // returns "Assessment"
LEFT(B5, FIND("_", B5) - 1) // left
The length parameter represents the total number of characters to extract from the current string instance. This includes the starting character found at index startIndex. In other words, the Substring method attempts to extract characters from index startIndex to index startIndex + length - 1.,This method does not modify the value of the current instance. Instead, it returns a new string with length characters starting from the startIndex position in the current string.,If the character or character sequence is not included in the end of the substring, the length parameter equals endIndex - startIndex, where endIndex is the return value of the IndexOf or IndexOf method.,If the character or character sequence is not included in the end of the substring, the length parameter equals endIndex - startIndex, where endIndex is the return value of the IndexOf or IndexOf method.
public:
System::String ^ Substring(int startIndex);
public:
System::String ^ Substring(int startIndex);
public string Substring(int startIndex);
public string Substring (int startIndex);
member this.Substring: int - > string
The following example demonstrates obtaining a substring from a string.
using namespace System;
using namespace System::Collections;
int main()
{
array<String^>^info = { "Name: Felica Walker", "Title: Mz.",
"Age: 47", "Location: Paris", "Gender: F"};
int found = 0;
Console::WriteLine("The initial values in the array are:");
for each (String^ s in info)
Console::WriteLine(s);
Console::WriteLine("\nWe want to retrieve only the key information. That is:");
for each (String^ s in info) {
found = s->IndexOf(": ");
Console::WriteLine(" {0}", s->Substring(found + 2));
}
}
// The example displays the following output:
// The initial values in the array are:
// Name: Felica Walker
// Title: Mz.
// Age: 47
// Location: Paris
// Gender: F
//
// We want to retrieve only the key information. That is:
// Felica Walker
// Mz.
// 47
// Paris
// F
string[] info = {
"Name: Felica Walker",
"Title: Mz.",
"Age: 47",
"Location: Paris",
"Gender: F"
};
int found = 0;
Console.WriteLine("The initial values in the array are:");
foreach(string s in info)
Console.WriteLine(s);
Console.WriteLine("\nWe want to retrieve only the key information. That is:");
foreach(string s in info) {
found = s.IndexOf(": ");
Console.WriteLine(" {0}", s.Substring(found + 2));
}
// The example displays the following output:
// The initial values in the array are:
// Name: Felica Walker
// Title: Mz.
// Age: 47
// Location: Paris
// Gender: F
//
// We want to retrieve only the key information. That is:
// Felica Walker
// Mz.
// 47
// Paris
// F
public:
System::String ^ Substring(int startIndex, int length);
public:
System::String ^ Substring(int startIndex, int length);
public string Substring(int startIndex, int length);
public string Substring (int startIndex, int length);
member this.Substring: int * int - > string
The following example illustrates a simple call to the Substring(Int32, Int32) method that extracts two characters from a string starting at the sixth character position (that is, at index five).
String value = "This is a string.";
int startIndex = 5;
int length = 2;
String substring = value.Substring(startIndex, length);
Console.WriteLine(substring);
// The example displays the following output:
// is
let value = "This is a string."
let startIndex = 5
let length = 2
let substring = value.Substring(startIndex, length)
printfn $ "{substring}"
// The example displays the following output:
// is
Module Example Public Sub Main() Dim s As String = "aaaaabbbcccccccdd" Dim charRange As Char = "b" c Dim startIndex As Integer = s.Indexof(charRange) Dim endIndex As Integer = s.LastIndexOf(charRange) Dim length = endIndex - startIndex + 1 Console.WriteLine("{0}.Substring({1}, {2}) = {3}", s, startIndex, length, s.Substring(startIndex, length)) End Sub End Module ' The example displays the following output: ' aaaaabbbcccccccdd.Substring(5, 3) = bbb
Module Example
Public Sub Main()
Dim s As String = "<term>extant<definition>still in existence</definition></term>"
Dim searchString As String = "<definition>"
Dim startindex As Integer = s.IndexOf(searchString)
searchString = "</" + searchString.Substring(1)
Dim endIndex As Integer = s.IndexOf(searchString)
Dim substring As String = s.Substring(startIndex, endIndex + searchString.Length - StartIndex)
Console.WriteLine("Original string: {0}", s)
Console.WriteLine("Substring; {0}", substring)
End Sub
End Module
' The example displays the following output:
' Original string: <term>extant<definition>still in existence</definition></term>
' Substring; <definition>still in existence</definition>
Select substrings from each element of a string array. When you specify different positions with numeric arrays, they must be the same size as the input string array.,If str is a string array or cell array of character vectors, then you can extract substrings from every element of str. You can specify that the substrings either all have the same start or have different starts in each element of str.,Create a new string array from the elements of a string array. When you specify different substrings as positions, they must be contained in a string array or a cell array that is the same size as the input string array.,Start position, specified as a numeric array. extractAfter excludes the character at pos from the extracted substring.
str = "The quick brown fox"
str =
"The quick brown fox"
newStr = extractAfter(str, "quick ")
newStr =
"brown fox"
str = ["The quick brown fox jumps";
"over the lazy dog"
]
str = 2 x1 string "The quick brown fox jumps"
"over the lazy dog"
You may combine the sub and substr functions for this as shown below. The last number in the second line defines the number of characters to be extracted (i.e. 6).,Let’s assume that we want to extract all characters of our character string before the pattern “xxx”. Then, we can use the sub function as follows:,Can I extract only a certain number of characters after the string of interest? I have a column with a ‘ecoscore’ formatted as follows: ,This time the sub function is extracting the words on the right side of our pattern, i.e. “other stuff”.
x < -"hello xxx other stuff" # Example character string x # Print example string # "hello xxx other stuff"
sub(" xxx.*", "", x) # Extract characters before pattern # "hello"
sub(".*xxx ", "", x) # Extract characters after pattern # "other stuff"
sub(" .*", "", x)
sub(".*?_", "", "I_love_R")
x < -"Ecosystem Classification: boreal forest; EcoScore: 3.5/5. Small glade like openings" x_new < -substr(sub(".*EcoScore: ", "", x), 1, 6) x_new # "3.5/5."