473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Search String for Word

What's the best way to search a string for a particular word and get a
booleen value indicating whether it exists in the string or not?
Thanks...

Jun 26 '06 #1
5 1215
> What's the best way to search a string for a particular word and get a
booleen value indicating whether it exists in the string or not?

substring = 'foo'
targetstring = 'blah foo bar'
substring in targetstring True if substring in targetstring: print 'yup'

yup

http://docs.python.org/lib/typesseq.html

-tkc


Jun 26 '06 #2
I thought I'd seen that somewhere! Thanks Tim. I was previously using
re.search(substring, targetstring).

Tim Chase wrote:
What's the best way to search a string for a particular word and get a
booleen value indicating whether it exists in the string or not?

>>> substring = 'foo'
>>> targetstring = 'blah foo bar'
>>> substring in targetstring True >>> if substring in targetstring: print 'yup'

yup

http://docs.python.org/lib/typesseq.html

-tkc


Jun 26 '06 #3
And what if I want to search for an item in a tuple, is there a
similarly easy method?

Tim Chase wrote:
What's the best way to search a string for a particular word and get a
booleen value indicating whether it exists in the string or not?

>>> substring = 'foo'
>>> targetstring = 'blah foo bar'
>>> substring in targetstring True >>> if substring in targetstring: print 'yup'

yup

http://docs.python.org/lib/typesseq.html

-tkc


Jun 26 '06 #4
On 26 Jun 2006 08:24:54 -0700, di*************@gmail.com
<di*************@gmail.com> wrote:
And what if I want to search for an item in a tuple, is there a
similarly easy method?

Tim Chase wrote:
What's the best way to search a string for a particular word and get a
booleen value indicating whether it exists in the string or not?

>>> substring = 'foo'
>>> targetstring = 'blah foo bar'
>>> substring in targetstring

True
>>> if substring in targetstring: print 'yup'

yup
t = ('a', 'b', 'c', 'd', 'e')
a = 'a'
a in t True y = 'y'
y in t False
t = ('test', 'black', 'white')
a = 'a'
[i for i in t if a in i] ['black']


HTH :)
Jun 26 '06 #5
Thank you thank you!

Tim Williams wrote:
On 26 Jun 2006 08:24:54 -0700, di*************@gmail.com
<di*************@gmail.com> wrote:
And what if I want to search for an item in a tuple, is there a
similarly easy method?

Tim Chase wrote:
> What's the best way to search a string for a particular word and get a
> booleen value indicating whether it exists in the string or not?

>>> substring = 'foo'
>>> targetstring = 'blah foo bar'
>>> substring in targetstring
True
>>> if substring in targetstring: print 'yup'
yup t = ('a', 'b', 'c', 'd', 'e')
a = 'a'
a in t True y = 'y'
y in t False
t = ('test', 'black', 'white')
a = 'a'
[i for i in t if a in i] ['black']


HTH :)


Jun 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
14
by: Lee Mundie | last post by:
Hi, wonder if anyone can help me with this - This was originally a simple search routine returning results back to itself! Trying to be smart I tried to combine a no. items per page so I could...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
7
by: Bonj | last post by:
In making a ternary search tree to identify as fast as possible the type of word passed in to the algorithm, for instance sp_help -> 1 (procedures), select -> 2 (keywords), sysobjects -> 3 (system...
5
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word...
36
by: lovecreatesbeauty | last post by:
Any comments are welcome for following the two sequential search C functions of both integer and string versions. Though they are simple, please do not laugh at it :) Could you give your great...
3
by: Russell | last post by:
Hey, ok i have numerous tables to search through for a 'site search'. some of the searchble fields have html embeded within so after some quick referencing, saw I can use the regExp function...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
2
by: caine | last post by:
I'm doing a search application for my project. My code can prompt alert popup window when the user doesn't key in any keywords. However, if the user keys in any keywords, it juz return "Please...
1
by: nganglove | last post by:
C++ string search -------------------------------------------------------------------------------- Hello, please can any one help me? I am given an assigment in C++ to read a text file and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.