How exactly can you take a string, split it, reverse it and join it back together again without the brackets, commas, etc. using python?
Using split()When the string is empty and no separator is specified, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.
The inverse of the split method is join . You choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements.
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
>>> tmp = "a,b,cde" >>> tmp2 = tmp.split(',') >>> tmp2.reverse() >>> "".join(tmp2) 'cdeba'
or simpler:
>>> tmp = "a,b,cde" >>> ''.join(tmp.split(',')[::-1]) 'cdeba'
The important parts here are the split function and the join function. To reverse the list you can use reverse()
, which reverses the list in place or the slicing syntax [::-1]
which returns a new, reversed list.
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