split() method splits a string into a LIST.

PHOTO EMBED

Wed Oct 26 2022 01:08:21 GMT+0000 (Coordinated Universal Time)

Saved by @L0uJ1rky45M #python

# string.split(separator, maxsplit)
	# separator is optional and refers to any whitespace as default. It indicates which separator to use when splitting the string
	# maxsplit is optional and specifies how many splits to do. Default value is -1, whichis "all occurrences"
----
# Below, we try to extract "stephen.marquard" from "From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008".
line = "From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008"
words = line.split() # words = ['From', 'stephen.marquard@uct.ac.za', 'Sat', 'Jan', '5', '09:14:16', '2008']
email = words[1] # email = stephen.marquard@uct.ac.za
pieces = email.split('@') # pieces = ['stephen.marquard', 'uct.ac.za']
print(pieces[1])
---
# setting the maxsplit parameter to 2, will return a list with 3 elements
line = "From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008"
words = line.split(' ', 2)
print(words) # ['From', 'stephen.marquard@uct.ac.za', 'Sat Jan  5 09:14:16 2008']
content_copyCOPY

You can use this method to extract a list from a string and split the list further.

https://www.py4e.com/html3/08-lists