Find and extract sub-strings
Fri Sep 23 2022 00:48:18 GMT+0000 (UTC)
Saved by
@L0uJ1rky45M
#python
# variable.find() searches for the position of one string within another. Values printed are the index of the substring. Outputs are in comment.
data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
atpos = data.find('@')
print(atpos)
# 21
sppos = data.find(' ',atpos)
print(sppos)
# 31
host = data[atpos+1:sppos]
print(host)
# uct.ac.za
content_copyCOPY
Use the find method and string slicing to find and pull out a sub-string (a shorter string within a longer string)
https://www.py4e.com/html3/06-strings
Comments