Answers for "split string into list of words python"

27

separate a string in python

foo = "A B C D"
bar = "E-F-G-H"

# the split() function will return a list
foo_list = foo.split()
# if you give no arguments, it will separate by whitespaces by default
# ["A", "B", "C", "D"]

bar_list = bar.split("-", 3)
# you can specify the maximum amount of elements the split() function will output
# ["E", "F", "G"]
Posted by: Guest on May-05-2020
0

separate words in a text to make a list python

your_text = "some text"
separated_text = your_text.split(" ") #the space is just an exemple but you can put anything
print(separated_text)

output:
['some', 'text']
Posted by: Guest on February-10-2021
0

split a string into an array of words in python

string = '1-2-3
array_of_words = string.split('-')
print(array_of_words)    # prints ['1', '2', '3']
##################################################
#if you dont pass anything in the split() function,
#the default parameter will be a space
##################################################
string = '1 2 3'
array_of_words = string.split()
print(array_of_words)    # prints ['1', '2', '3']
Posted by: Guest on September-30-2021

Code answers related to "split string into list of words python"

Python Answers by Framework

Browse Popular Code Answers by Language