0
string = "Hi how are you"
reversedString = []
mysplit = string.split(" ")
for i in mysplit:
    reverse = i[::-1]
    print reverse
    final_List =  reversedString.append(reverse)
print final_List

Result i am getting is: iH woh era uoy None

Why i am getting "None" when try to append the reversed string.Please help

Paranth
  • 11
  • 2
  • Because append doesn't return anything. Just do `reversedString.append(reverse)` and print `reversedString` in the end. – L3viathan Mar 09 '16 at 10:59
  • `final_List` is not needed here since `reversedString` already has the list. `final_List` is none because the return type of `append ` is none – The6thSense Mar 09 '16 at 11:00
  • Make a list comprehension like: ``res = ' '.join([i[::-1] for i in string.split()])`` that creates one complete string: ``'iH woh era uoy'``. But your problem was that you printed ``final_List`` which is just ``None`` because ``append`` doesn't return anything. – MSeifert Mar 09 '16 at 11:06

2 Answers2

1

list.append(item) modifies the list in place and returns None.

Just print reversedString:

string = "Hi how are you"
reversedString = []
mysplit = string.split(" ")
for i in mysplit:
    reverse = i[::-1]
    print reverse
    reversedString.append(reverse)
print reversedString

Also, string.split(' ') splits at each space character, so if you have string = 'foo bar', you'd get the list ['foo', '', '', 'bar']; if you want to separate words with any amount of whitespace in between, just use string.split().

0
string = "Hi how are you"
reversedString = []
mysplit = string.split(" ")

for i in mysplit:
    reverse = i[::-1]
    reversedString.append(reverse)

print  reversedString   

output:

['iH', 'woh', 'era', 'uoy']
roadrunner66
  • 7,772
  • 4
  • 32
  • 38