I have read the documentation but don't fully understand how to do it.
I understand that I need to have some kind of identifier in the string so that the functions can find where to split the string (unless I can target the first space in the sentence?).
So for example how would I split:
"Sico87 is an awful python developer"
to "Sico87"
and "is an awful Python developer"
?
The strings are retrieved from a database (if this does matter).
Use the split
method on strings:
>>> "Sico87 is an awful python developer".split(' ', 1)
['Sico87', 'is an awful python developer']
How it works:
split
in this case. You call them using obj.<methodname>(<arguments>)
.split
is the character that separates the individual substrings. In this case that is a space, ' '
.The second argument is the number of times the split should be performed. In your case that is 1
. Leaving out this second argument applies the split as often as possible:
>>> "Sico87 is an awful python developer".split(' ')
['Sico87', 'is', 'an', 'awful', 'python', 'developer']
Of course you can also store the substrings in separate variables instead of a list:
>>> a, b = "Sico87 is an awful python developer".split(' ', 1)
>>> a
'Sico87'
>>> b
'is an awful python developer'
But do note that this will cause trouble if certain inputs do not contain spaces:
>>> a, b = "string_without_spaces".split(' ', 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With