Skip to content

Commit b6b5024

Browse files
committed
Added string_find.py
1 parent 423129a commit b6b5024

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

section_02_(strings)/string_find.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# String methods: string.find()
2+
3+
# string.find() tells you where you can find a part of one string in a larger string.
4+
# string.find() will return a number:
5+
# if string.find() returns -1, it could not find the string inside the larger string.
6+
# otherwise, string.find() will return the slicing number/index of where it found that string
7+
8+
email_address = "[email protected]"
9+
10+
print "I found the snail at: {0}".format(email_address.find("@")) # the slicing number/index of where the at symbol appears
11+
12+
# string.find() + slicing = awesome!
13+
14+
# Everything before the @ is part of the email_handle; everything after the @ is part of the domain where they have their email registered.
15+
# Let's use string.find() and slicing together to split those apart.
16+
17+
at_symbol_index = email_address.find("@")
18+
19+
print "I found the snail at: {0}".format(at_symbol_index) # Notice how line 10 and 19 each give the same result, but take a different approach
20+
21+
email_handle = email_address[0:at_symbol_index]
22+
23+
print "The email_handle is: {0}".format(email_handle)
24+
25+
email_domain = email_address[at_symbol_index + 1:] # without the +1, the at symbol would be included. Notice that there is no number after the colon, so Python assumes you want everything to the end.
26+
27+
print "The email_domain is: {0}".format(email_domain)
28+
29+
print "When string.find() can't find a string, it'll give a -1. So since there's no 'QQQ' in email_address, this will return a -1: {0}".format(email_address.find("QQQ"))

0 commit comments

Comments
 (0)