4

I'm very new to coding and need help on one last question of an assignment that has me stumped. I can't use regular expressions for this assignment, either.

I've got this string, and I've made it so I split the string after 'cat' occurs.

astr = 'accaggcatgattgcccgattccatgcggtcag'
x = astr.split('cat',1)[-1]
print(x)
gattgcccgattccatgcggtcag
y = astr.split('cat',2)[-1]
print(y)
gcggtcag

However, what can I do if I only want the three letters after each 'cat' in the string? For example, I'd want to get 'gat' and 'gcg'.

Any help is greatly appreciated!

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Summer
  • 43
  • 2

2 Answers2

3

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:])))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    What is the purpose of the `[-1]`? I am trying to solve problems to learn python (VBA mod here) and I tried to use the `find` method to solve this but would like to understand your solution – urdearboy Mar 04 '19 at 03:06
  • @urdearboy The purpose of it is to get the last element, so `[-2]` is second last, and `[-3]` is third last and so on.. here we're doing to a list, so last element of the list, also see [this hot question](https://stackoverflow.com/questions/509211/understanding-slice-notation) – U13-Forward Mar 04 '19 at 03:09
  • Oh boy. I can tell that VBA being my first language is going to make this language hard to learn. I keep going for end results how I would in VBA and then I try to compare what I did to what others post and the methods are so different. *bless stack overflow for endless practice problems with endless answer lists* – urdearboy Mar 04 '19 at 03:30
1

You can also get all of them in one go:

[x[:3] for x in astr.split('cat')[1:]]

Output:

['gat', 'gcg']
busybear
  • 10,194
  • 1
  • 25
  • 42