Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy

Sunday, 23 December 2012

Searching for a sub string in a String

December 23, 2012 Posted by Dinesh No comments


It is usual to check for an item in a list, tuple, or dict using 'in' operator.

List=[ 'l' , 'i' , 's' , 't']
if 's' in List:
    print "success"

for string we usually go with 'find' function. Its quite easier to go with 'in' operator for string too.
#!/usr/bin/python
str1 = "this is string example";
str2 = "is";
if str1.find(str2) != -1:
print "found"
#Its quite easier to go with 'in' operator for string too.
if str2 in str1:
print "found"
view raw string_find.py hosted with ❤ by GitHub


0 comments:

Post a Comment