I’m trying to reverse a string and using the below code but the resultant reverse list value is None.
The code:
str_a = 'This is stirng'
rev_word = str_a.split()
rev_word = rev_word.reverse()
rev_word = ''.join(rev_word)
It returns the TypeError. Why?
.reverse()returnsNone. Therefore you should not be assigning it to a variable.Use this instead:
I’ve run the code on IDEOne for you so you can see the output. (Also notice that the output is
stringaisThis; you may want to use' '.join(revword), with a space, instead.)Also note that the method you have provided only reverses the words, not the text. @ron.rothman provided a link that does detail how to reverse a string in its entirety.