Why does this not reverse the text that is inputted: text = input() rev_text = ' '.join(text.split()[::-1]) print(rev_text) | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Why does this not reverse the text that is inputted: text = input() rev_text = ' '.join(text.split()[::-1]) print(rev_text)

12th Oct 2016, 1:50 AM
Slayer
Slayer - avatar
7 Antworten
+ 1
text.split() splits at whitespace. it should reverse the order words appear but not the letters in the words. not sure this will work, but you can try rev_text = "".join(c for c in text[-1:0:-1]) something along those lines should work, since you can slice strings.
12th Oct 2016, 2:07 AM
Luke Armstrong
+ 1
As Luke said split() splits at whitespaces. For example: s = "abc def" print(s.split()) The result will be: ['abc', 'def'] You can reverse strings easily: rev_str = str[::-1]
12th Oct 2016, 8:23 PM
novice
0
ok ill try that out thx
28th Oct 2016, 2:10 AM
Slayer
Slayer - avatar
0
both of them didnt work
28th Oct 2016, 2:31 AM
Slayer
Slayer - avatar
0
luke you were pretty close actually
28th Oct 2016, 2:32 AM
Slayer
Slayer - avatar
0
you got it working but no output
28th Oct 2016, 2:32 AM
Slayer
Slayer - avatar
0
text = input() print = rev_text = "input".join(c for c in text[-1:0:-1])
28th Oct 2016, 2:33 AM
Slayer
Slayer - avatar