Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66713 > unrolled thread

Insert variable into text search string

Started byKevin Glover <kevingloveruk@gmail.com>
First post2014-02-19 11:39 -0800
Last post2014-02-20 13:06 +1100
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Insert variable into text search string Kevin Glover <kevingloveruk@gmail.com> - 2014-02-19 11:39 -0800
    Re: Insert variable into text search string MRAB <python@mrabarnett.plus.com> - 2014-02-19 19:46 +0000
    Re: Insert variable into text search string Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-02-19 21:59 +0200
    Re: Insert variable into text search string Kevin Glover <kevingloveruk@gmail.com> - 2014-02-19 12:15 -0800
      Re: Insert variable into text search string Chris Angelico <rosuav@gmail.com> - 2014-02-20 13:06 +1100

#66713 — Insert variable into text search string

FromKevin Glover <kevingloveruk@gmail.com>
Date2014-02-19 11:39 -0800
SubjectInsert variable into text search string
Message-ID<7a20fe74-e689-4261-b608-73d0f49736f3@googlegroups.com>
These two lines are from a program that carries out a phrase search of Wikipedia and returns the total number of times that the specific phrase occurs. It is essential that the search contains an apostrophe:

results = w.search("\"of the cat's\"", type=ALL, start=1, count=1)
print results.total

I want to replace the word "cat" with a variable, e.g.

q = "cat"

so that I can generate the same search format for a list of different words. How do I format the search string to include the variable, please? I am using Python 2.7.

[toc] | [next] | [standalone]


#66714

FromMRAB <python@mrabarnett.plus.com>
Date2014-02-19 19:46 +0000
Message-ID<mailman.7162.1392839218.18130.python-list@python.org>
In reply to#66713
On 2014-02-19 19:39, Kevin Glover wrote:
> These two lines are from a program that carries out a phrase search of Wikipedia and returns the total number of times that the specific phrase occurs. It is essential that the search contains an apostrophe:
>
> results = w.search("\"of the cat's\"", type=ALL, start=1, count=1)
> print results.total
>
> I want to replace the word "cat" with a variable, e.g.
>
> q = "cat"
>
> so that I can generate the same search format for a list of different words. How do I format the search string to include the variable, please? I am using Python 2.7.
>
Try string concatenation:

     results = w.search("\"of the " + q + "'s\"", type=ALL, start=1, 
count=1)

[toc] | [prev] | [next] | [standalone]


#66715

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2014-02-19 21:59 +0200
Message-ID<qotmwhmkh5g.fsf@ruuvi.it.helsinki.fi>
In reply to#66713
Kevin Glover writes:

> These two lines are from a program that carries out a phrase search
> of Wikipedia and returns the total number of times that the specific
> phrase occurs. It is essential that the search contains an
> apostrophe:
> 
> results = w.search("\"of the cat's\"", type=ALL, start=1, count=1)
> print results.total
> 
> I want to replace the word "cat" with a variable, e.g.
> 
> q = "cat"
> 
> so that I can generate the same search format for a list of
> different words. How do I format the search string to include the
> variable, please? I am using Python 2.7.

Format is the right word: look up the .format method of strings.

   >>> '''"of the {q}'s"'''.format(q='kangaroo')
   '"of the kangaroo\'s"'

That's with Python 2.7.3. An older mechanism uses the percent sign for
place holders and as an operator.

[toc] | [prev] | [next] | [standalone]


#66716

FromKevin Glover <kevingloveruk@gmail.com>
Date2014-02-19 12:15 -0800
Message-ID<6e8fa7a0-f943-4802-aedd-4672ad18c352@googlegroups.com>
In reply to#66713
Thank you both so much. I had tried % but not successfully.

[toc] | [prev] | [next] | [standalone]


#66728

FromChris Angelico <rosuav@gmail.com>
Date2014-02-20 13:06 +1100
Message-ID<mailman.7170.1392861986.18130.python-list@python.org>
In reply to#66716
On Thu, Feb 20, 2014 at 7:15 AM, Kevin Glover <kevingloveruk@gmail.com> wrote:
> Thank you both so much. I had tried % but not successfully.

To do it with %, just do this:

whatever = "cat"
results = w.search("\"of the %s's\""%whatever, type=ALL, start=1, count=1)

Use either that or .format(), whichever you like - both are fully
supported, and they have different strengths.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web