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 split list

string = 'this is a python string'
wordList = string.split(' ')
Comment

python split list

list = [11, 18, 19, 21]

length = len(list)

middle_index = length // 2

first_half = list[:middle_index]
second_half = list[middle_index:]

print(first_half)
print(second_half)
Comment

python spliting string into list

text = 'This is python!
x = text.split()
print(x)
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

python split list

string = "this is a string" 		# Creates the string
splited_string = string.split(" ")	# Splits the string by spaces
print(splited_string) 				# Prints the list to the console
# Output: ['this', 'is', 'a', 'string']
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 :: python online practice test 
Python :: what is queryset in django 
Python :: python save picture in folder 
Python :: text to image python 
Python :: python timeit function return value 
Python :: python logging level 
Python :: objects.filter django 
Python :: w=how to tell if decimal in python 
Python :: python print variable and string 
Python :: python easter egg 
Python :: TfidfVectorizer use 
Python :: Math Module log10() Function in python 
Python :: Get the first 4 numbers of the innermost arrays using numpy 
Python :: streamlit - Warning: NumberInput value below has type int so is displayed as int despite format string %.1f. 
Python :: every cell change comma to point pandas 
Python :: modules in python 
Python :: hex string to hex number 
Python :: python check if attribute exists in dictionary 
Python :: from string to flaot python numpy 
Python :: Mac: Access your iCloud Documents folder with Jupyter Notebook or JupyterLab 
Python :: dataframe to csv 
Python :: python data insert 
Python :: cv2 and PIL BRG to RGB 
Python :: argsort in descending order numpy 
Python :: Import "sendgrid" could not be resolved django 
Python :: pandas append new column 
Python :: python find index 
Python :: check for changed model fields in djnago signal 
Python :: python trace code execution 
Python :: flask sqlalchemy case insensitive like 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =