feature request: string.contains('...')

Peter Otten __peter__ at web.de
Fri Sep 24 04:21:47 EDT 2010


Wim Feijen wrote:

> I was wondering, how to make a feature request?

You can post suggestions to improve python on the python-ideas mailing list 
or make feature requests on the bugtracker at bugs.python.org

> I would really like having a string.contains('...') function which returns
> either True or False. I know I can mimick this behaviour by saying
> string.find('...') != -1 , however, I find this harder to read.
> 
> string.contains('...') is easier to understand and more closely resembles
> natural language.

In modern python those functions provided by the string module that are also 
available as methods should not be used anymore. Instead of

string.find(s, substr) 

write

s.find(substr)

If you are not interested in the position of the substr use the "in" 
operator:

if substr in s:
    print "found"
else:
    print "not found"

Peter




More information about the Python-list mailing list