The Python string split() method lets us split a string into a list.
One example where you need this is if you process a CSV file. CSV files typically use a delimiter like a comma (,) or a semicolon (;) to separate entries.
If you want to read a CSV file (or any other file) in Python, you can find an explanation in the Python read and write files lesson. In this lesson, we’ll focus on how to split strings.
We use the split() method by specifying the string, followed by .split() , and a separator between the parentheses. If you don’t specify a separator, the default separator is a space.
Let’s try to split a string which uses a comma as the delimiter:
The split() method converts our string into a list, using the comma as the separator, and returns a list with three strings. Let’s try one more example:
Hi Rene,
as per this eg:
https://cdn-forum.networklessons.com/uploads/default/original/2X/4/4e66446616dc81bce9513e3697772001e35b3c89.png
'The stored output of the split() method must be this [‘R1’, ‘IOS-XE’, ‘192.168.1.1’] not this one csv_line = “R1,IOS-XE,192.168.1.1”.
please clarify
Hello Pradyumna
If the variable csv_line is a string, then the output of that variable in the window on the right will be of the form “R1,IOS-XE,192.168.1.1”.
If the variable csv_line is a list, then the output of that variable in the window on the right will be of the form [‘R1’, ‘IOS-XE’, ‘192.168.1.1’].
Notice in the following screenshot, initially, the output of csv_line is with quotes, and separated by commas. After it is converted, it appears in square brackets, with each item of the list in single quotes, and separated by commas.
https://cdn-forum.netwo
... Continue reading in our forum