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


Groups > comp.lang.python > #66714

Re: Insert variable into text search string

Date 2014-02-19 19:46 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Insert variable into text search string
References <7a20fe74-e689-4261-b608-73d0f49736f3@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.7162.1392839218.18130.python-list@python.org> (permalink)

Show all headers | View raw


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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web