Use the str.split method:
>>> "MATCHES__STRING".split("__")
['MATCHES', 'STRING']
Answer from adamk on Stack Overflowsplit() function splitting every character?
Split a string by a delimiter in Python - Stack Overflow
python - How do I split a string into a list of characters? - Stack Overflow
Trying to understand string split()
Every other time I have used the split function, it splits at the spaces but this time it is different. Here is my code:
c = "add 17 92 403" c.split() print(c[0])
When I run the code, the output is just the 'a'. I have no idea why this is happening. Do you?
Use the str.split method:
>>> "MATCHES__STRING".split("__")
['MATCHES', 'STRING']
Besides split and rsplit, there is partition/rpartition. It separates string once, but the way question was asked, it may apply as well.
Example:
>>> "MATCHES__STRING".partition("__")
('MATCHES', '__', 'STRING')
>>> "MATCHES__STRING".partition("__")[::2]
('MATCHES', 'STRING')
And a bit faster then split("_",1):
$ python -m timeit "'validate_field_name'.split('_', 1)[-1]"
2000000 loops, best of 5: 136 nsec per loop
$ python -m timeit "'validate_field_name'.partition('_')[-1]"
2000000 loops, best of 5: 108 nsec per loop
Timeit lines are based on this answer
Use the list constructor:
>>> list("foobar")
['f', 'o', 'o', 'b', 'a', 'r']
list builds a new list using items obtained by iterating over the input iterable. A string is an iterable -- iterating over it yields a single character at each iteration step.
You take the string and pass it to list()
s = "mystring"
l = list(s)
print l
Hi,
I am unable to wrap my head around how string split() works in Python. Consider the following examples:
β/β.split(β/β) # Outputs [ββ, ββ]
β a b β.split() # Outputs [βaβ, βbβ]
I was expecting the second example to return [ββ, βaβ, βbβ, ββ]. Why does split() omit the empty strings in the output list?
You can do this with strings (and lists) using slicing:
string = "hello world!"
splitat = 4
left, right = string[:splitat], string[splitat:]
will result in:
>>> left
hell
>>> right
o world!
Maybe the simplest solution is to use string slicing:
test = 'annamarypeterson, Guest Relations Manager, responded to this reviewResponded 1 week agoDear LoreLoreLore,Greetings from Amsterdam!We have received your wonderful comments and wanted to thank you for sharing your positive experience with us. We are so thankful that you have selected the Andaz Amsterdam for your special all-girls weekend getaway. Please come and see us again in the near future and let our team pamper you and your girlfriends!!Thanks again!Anna MaryAndaz Amsterdam -Guest RelationsReport response as inappropriateThank you. We appreciate your input.This response is the subjective opinion of the management representative'
pos = test.find('ago') + 3
print(test[:pos], test[pos:])
Luckily, Python has this built-in :)
import re
# Regex pattern splits on substrings "; " and ", "
re.split('; |, ', string_to_split)
Update:
Following your comment:
>>> string_to_split = 'Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n', string_to_split)
['Beautiful', 'is', 'better', 'than', 'ugly']
Do a str.replace('; ', ', ') and then a str.split(', ')