Insert variable into text search string
MRAB
python at mrabarnett.plus.com
Wed Feb 19 14:46:50 EST 2014
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)
More information about the Python-list
mailing list