0

How can I select a string in python knowing the start and end points?

If the string is:

Evelin said, "Hi Dude! How are you?" and no one cared!!

Or something like this:

Jane said *aww! thats cute, we must try it!* John replied, "Okay!, but not now!!"

what I want to write is a function that selects from the " " not by counting the index,
but something that just selects the text from character to character,

"Hi Dude! How are you?" and "Okay!, but not now!!"

so how can I do this? is there a built in function ?

I know there is a built-in function in python that get the index of the given character

ie,

find("something") returns the index of the given string in the string.

or it need to loop through the string?

I'm just starting with python, sorry for a little question like this. python 2 or 3 is just okay!! thank you so much!!

Update:

Thank you everyone for the answers, as a just beginner I just wanna stick with the built in split() function quotes = string.split('"')[1::2] just because its simple. thank you all. so much love :)

Community
  • 1
  • 1

4 Answers4

1
txt='''\
Evelin said, "Hi Dude! How are you?" and no one cared!!
Jane said *aww! thats cute, we must try it!* John replied, "Okay!, but not now!!"'''

import re

print re.findall(r'"([^"]+)"', txt)
# ['Hi Dude! How are you?', 'Okay!, but not now!!']
dawg
  • 98,345
  • 23
  • 131
  • 206
0

You can use regular expressions if you don't want to use str.index():

import re

quotes = re.findall('"([^"]*)"', string)

You can easily extend this to extract other information from your strings as well.

Alternatively:

quotes = string.split('"')[1::2]

And using str.index():

first = string.index('"')
second = string.index('"', first+1)
quote = string[first+1:second]
Scorpion_God
  • 1,499
  • 10
  • 15
  • i loved the `quotes = string.split('"')[1::2]` method its much simple, workdone without any imports. btw what is this `[1::2]` ? thaank you –  Aug 30 '14 at 16:26
  • 1
    Have a look at [this](http://stackoverflow.com/questions/509211/pythons-slice-notation). Basically, I take every second element, starting with the first one. You can just use `[1]` instead if there's only one quote in the string. – Scorpion_God Aug 30 '14 at 16:28
  • Carefully with the regex of `'"(.*?)"'` It [won't match](http://regex101.com/r/cF0dM9/1) multiline quotes unless you use `re.S` flag. – dawg Aug 30 '14 at 16:40
0

To extract a substring by characters, it is much easier to split on those characters; str.partition() and str.rpartition() efficiently split the string on the first or last occurrence of a given string:

extracted = inputstring.partition('"')[-1].rpartition('"')[0]

The combination of partitioning from the start and end gives you then the largest substring possible, leaving any embedded quotes in there.

Demo:

>>> inputstring = 'Evelin said, "Hi Dude! How are you?" and no one cared!!'
>>> inputstring.partition('"')
('Evelin said, ', '"', 'Hi Dude! How are you?" and no one cared!!')
>>> inputstring.rpartition('"')
('Evelin said, "Hi Dude! How are you?', '"', ' and no one cared!!')
>>> inputstring.partition('"')[-1].rpartition('"')[0]
'Hi Dude! How are you?'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

str.index(str2) finds the index of str2 in str... most simple approach !

a = 'Evelin said, "Hi Dude! How are you?" and no one cared!!'
print a[1+a.index("\""):1+a.index("\"")+a[a.index("\"")+1:].index("\"")]

or as Scorpion_God mentioned, you could simply use single quotes as below

print a[1+a.index('"'):1+a.index('"')+a[a.index('"')+1:].index('"')]

this will result in :

Hi Dude! How are you?

Quotes won't be included !!!

mlwn
  • 1,156
  • 1
  • 10
  • 25
  • 1
    @Scorpion_God, you could always use more variables and split the expression into extremely simple expressions... I just combined all in one line – mlwn Aug 30 '14 at 15:54
  • 1
    I knew you were one of those kind people who like to be nice to the other people reading their code. You don't need to have all those back slashes if you use single quotes. – Scorpion_God Aug 30 '14 at 15:57
  • Also, using more variables, like I have done in my answer, makes it much clearer as to what you're trying to achieve. And you don't need to call `str.index()` as many times. – Scorpion_God Aug 30 '14 at 16:04