Python Split String into Multiple String

Split string with multiple delimiters in Python

Luckily, Python has this built-in :)

import re
re.split('; |, ', string_to_split)

Update:
Following your comment:

>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']

Split string into two parts only

You could use a,b = split(' ', 1).

The second argument 1 is the maximum number of splits that would be done.

s = 'abcd efgh hijk'
a,b = s.split(' ', 1)
print(a) #abcd
print(b) #efgh hijk

For more information on the string split function, see str.split in the manual.

Split Strings into words with multiple word boundary delimiters

A case where regular expressions are justified:

import re
DATA = "Hey, you - what are you doing here!?"
print re.findall(r"[\w']+", DATA)
# Prints ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here']

Python: how to split a string by multiple strings

You can use regex to split based on any combinations of uppercase letters with len 1 or more :

>>> tr = "apple AND orange OR banana"
>>> re.split(r'[A-Z]+',tr)
['apple ', ' orange ', ' banana']

But if you just want to split with AND or OR :

>>> re.split(r'AND|OR',tr)
['apple ', ' orange ', ' banana']

And for remove the spaces if you are sure that your sentence are contain distinc words you can do :

>>> re.split(r'[A-Z ]+',tr)
['apple', 'orange', 'banana']

If you have a AND or OR in leading or trailing of your string using split will create a empty string in result , for get ride of that you can loop over splited list and check for validation of items, but as a more elegant way you can use re.findall :
with r'[^A-Z ]+' as its pattern :

>>> tr = "AND apple AND orangeOR banana"
>>> re.split(r'\s?(?:AND|OR)\s?',tr)
['', 'apple', 'orange', 'banana']
>>> re.split(r'[A-Z ]+',tr)
['', 'apple', 'orange', 'banana']
>>> [i for i in re.split(r'[A-Z ]+',tr) if i]
['apple', 'orange', 'banana']
>>> re.findall(r'[^A-Z ]+',tr)
['apple', 'orange', 'banana']

Python split string into multiple parts

You can make use of str.split with maxsplit argument controlling the number of splits. Then you can strip the comma and brackets from lat and lang:

>>> text = "[22.190894440000001, -100.99684750999999] 6 2011-08-28 19:48:11 @Karymitaville you can dance you can give having the time of my life(8) ABBA :D"
>>> lat, lang, value, date, time, text = text.split(maxsplit=5)
>>> lat, lang, value, date, time, text
('[22.190894440000001,', '-100.99684750999999]', '6', '2011-08-28', '19:48:11', '@Karymitaville you can dance you can give having the time of my life(8) ABBA :D')
>>> lat = lat.strip('[').rstrip(',')
>>> lang = lang.rstrip(']')
>>> lat, lang, value, date, time, text
('22.190894440000001', '-100.99684750999999', '6', '2011-08-28', '19:48:11', '@Karymitaville you can dance you can give having the time of my life(8) ABBA :D')

python - splitting a string in a list into multiple strings

strings = ['abc efg hijklmn aaaaa']
strings = strings[0].split()

Splitting a string that is converted to a list with a single item into multiple items

str.split(separator, maxsplit) method returns a list of strings after breaking the given string by the specified separator.

separator: The is a delimiter. The string splits at this specified separator. It is not provided then any white space is a separator.

maxsplit: It is a number, which tells us to split the string into a maximum of the provided number of times. If it is not provided then there is no limit.

You can do this if you want to split it into a list of single characters.

x='ABCDEFG'
char_list=list(x)
#['A', 'B', 'C', 'D', 'E', 'F', 'G']

If you want to split it into single characters avoiding any spaces then try this.

x='abcde fgh   i'
out=[char for string in x.split() for char in string]
#['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

And .split() doesn't convert your string to a list. In your, it returned ['ABCDEFG] because the string contains no spaces.



Related Topics



Leave a reply



Submit