4 Replies - 16937 Views - Last Post: 03 May 2010 - 12:07 AM Rate Topic: -----

#1 dstudentx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 04-December 09

check if line does not contain specific text

Posted 02 May 2010 - 12:38 PM

How can I check and see if the line contains specific text. I know how to check if it does contain text

line.startswith('Hello') 


How can I check and see if it does not contain 'hello'?

a c++ example would be

if (line != "Hello"){
cout<<" wrong ";
}
whats the equivalent in python?

This post has been edited by dstudentx: 02 May 2010 - 12:54 PM

Is This A Good Question/Topic? 0
  • +

Replies To: check if line does not contain specific text

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: check if line does not contain specific text

Posted 02 May 2010 - 06:45 PM

Well first of all using startswith only would work if the line starts with the word, not exactly if the line contains the word. For instance, your startswith('Hello') would not work for "Hi and Hello to you!" since the string does not start with 'Hello'. What you need to use are the functions find() or index(). The difference is that find() will return -1 if the string is not found in the text and index() will throw a "ValueError" in the same case.

If either of these two methods do find the match string in the text, they will return a value that is >= 0.

# Returns 7 because character index 7 is where 'hello' starts
'Hi and hello to you'.find('hello')

# Returns -1 because 'hello' is not found
'Hi and howdy to you all'.find('hello')

# Throws a ValueError because 'hello' is not found
'Hi and howdy to you all'.index('hello')



These functions search the string from the left. You can also start your search from the right using rfind() and rindex() as well. So be sure to look up all these functions to see how to use them. They also take optional start/end parameters too that you may find of interest.

Enjoy!


"At DIC we be python string finding code ninjas... 'Awesome, Cool, kicks ass'.find('DIC')... it never returns -1!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 dstudentx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 04-December 09

Re: check if line does not contain specific text

Posted 02 May 2010 - 08:27 PM

Wow you are a Ninja

Im trying to use it in a if statement and if it can't find Hello the is statement is not run, how do I do that?
for me it seems to run no matter what

 if line.find('Hello /'):
		found = True      
	    	print 'Booyah'

Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: check if line does not contain specific text

Posted 03 May 2010 - 12:05 AM

dstudentx said:

How can I check and see if it does not contain 'hello'?

>>> s = "abc Hello def"
>>> 'Hello' in s
True
>>> 'abc' in s
True
>>> 'ghi' in s
False
>>> 'ghi' not in s
True
>>>


one more way
Was This Post Helpful? 0

#5 Guest_c.user*


Reputation:

Re: check if line does not contain specific text

Posted 03 May 2010 - 12:07 AM

ok

This post has been edited by c.user: 03 May 2010 - 12:08 AM

Was This Post Helpful? 0

Page 1 of 1