Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

split a string into a list in python

line_1 = 'This is my line'
# When splitting it looks for spaces in the string and split from there
print(line_1.split())           # Output: ['This', 'is', 'my', 'line']
# Remember several spaces is also considered as one space
line_2 = 'This is            my line'
print(line_2.split())           # Output: ['This', 'is', 'my', 'line']
# We can also split the string using a sub-string (you can specify the delimiter)
# Now the spaces are not considered, look at 'm   y'
line_3 = 'This,is,m   y,line'
print(line_3.split(','))        # Output: ['This', 'is', 'my', 'line']
Source by www.py4e.com #
 
PREVIOUS NEXT
Tagged: #split #string #list #python
ADD COMMENT
Topic
Name
2+9 =