Search
 
SCRIPT & CODE EXAMPLE
 

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']
Comment

python spliting string into list

text = 'This is python!
x = text.split()
print(x)
Comment

list split to string

# instead of regular split
>> s = "a,b,c,d"
>> s.split(",")
>> ['a', 'b', 'c', 'd']

# ..split only on last occurrence of ',' in string:
>>> s.mysplit(s, -1)
>>> ['a,b,c', 'd']
Comment

split string to list

>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
Comment

python split by list

def split(txt, seps):
    default_sep = seps[0]
    # we skip seps[0] because that's the default separator
    for sep in seps[1:]:
        txt = txt.replace(sep, default_sep)
    return [i.strip() for i in txt.split(default_sep)]


>>> split('ABC ; DEF123,GHI_JKL ; MN OP', (',', ';'))
['ABC', 'DEF123', 'GHI_JKL', 'MN OP']
Comment

split() list

# Python program to take space
# separated input as a string
# split and store it to a list
# and print the string list
  
# input the list as string
string = input("Enter elements (Space-Separated): ")
  
# split the strings and store it to a list
lst = string.split()  
print('The list is:', lst)   # printing the list
Comment

PREVIOUS NEXT
Code Example
Python :: How do I plot a csv file in Jupyter notebook? 
Python :: set default formatter for python vscode 
Python :: Append a line to a text file using the write() function 
Python :: how to convert string to float in python 
Python :: session has key python 3 
Python :: call matlab function from python 
Python :: django show image in admin page 
Python :: creating methods in python 
Python :: binary search recursive python 
Python :: how to duplicate a column pandas 
Python :: how delete element from list python 
Python :: remove columns that start with pandas 
Python :: can a function output be save as a variable python 
Python :: python check if string is url 
Python :: mongoengine 
Python :: how to end a while loop python 
Python :: change python from 3.8 to 3.7 
Python :: filter query objects by date range in Django 
Python :: how to get text of a tag in selenium python 
Python :: python linux script 
Python :: python - How to execute a program or call a system command? 
Python :: demonstrating polymorphism in python class 
Python :: tkinter add text to canvas 
Python :: steps in for loop python 
Python :: how to join basename and directory in python os 
Python :: create frequency tables in pandas 
Python :: format timedelta python 
Python :: python print exection type 
Python :: python var power of 2 
Python :: python ip address increment 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =