-
Notifications
You must be signed in to change notification settings - Fork 0
/
findString.py
26 lines (21 loc) · 879 Bytes
/
findString.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# finding strings within strings by using find()
print "Example 1: Finding substrings in a string"
print "test".find("te")
print "test".find("st")
print "test"[2:]
print "Example 2: Finding substrings in a string which is stored as a variable"
my_string = "test"
print my_string.find("te")
print my_string.find("st")
print my_string[2:]
print "Example 3: Printing out everything after a certain substring"
my_string = "My favorite color: blue"
color_start_location = my_string.find("color:")
favorite_color = my_string[color_start_location:]
print favorite_color # oops, this line prints out 'color: ' as well...
print favorite_color[7:] # this fixes it!
print "Example 4: Other interesting things about string.find()..."
print "text".find("text") # prints 0
print "text".find("Text") # prints -1
print "text".find("") # prints 0
print "text".find(" ") # prints -1