0

I want to Split a string, reverse it and join it back with an X instead of a space in a function.

eg.

'WHO WATCHES THE WATCHERS -> to -> SEHCTAWXOHWXSREHCTAWXEHT'

str = ("WHO WATCHES THE WATCHERS")
str = str.split(' ',4)
str.reverse()
print str

str = 'X'
seq = ("who", "watches", "the", "watchers")
print str.join(seq)

But that only gets me:

['WATCHERS', 'THE', 'WATCHES', 'WHO']

whoXwatchesXtheXwatchers

when I want "SEHCTAWXOHWXSREHCTAWXEHT"

Please reply in simple function code. I'm a beginner at this.

lowlyf
  • 5
  • 1
  • 4
  • 2
    You are reusing the name `str` and smashing the old values. Take a look at your code carefully. It's a bad variable name anyway as you mask the built-in `str()` function. – Gareth Latty Apr 13 '13 at 23:01
  • This seems to be a popular question :=) – Ned Deily Apr 13 '13 at 23:03
  • 2
    Wait, how do you get "SEHCTAWXOHWXSREHCTAWXEHT" from "WHO WATCHES THE WATCHERS"? Wouldn't the expected output be "SREHCTAWXEHTXSEHCTAWXOHW"? – Emily Apr 13 '13 at 23:04
  • It's worth noting there is no need to put brackets around your string literals. – Gareth Latty Apr 13 '13 at 23:06
  • @Lattyware Yep I totally agree, it was just a quick thing but I have since changed in my code. Thanks – lowlyf Apr 13 '13 at 23:16
  • @NedDeily True, but I was after something a bit more simple. I was confused on that answer – lowlyf Apr 13 '13 at 23:17
  • @Emily Okay that is because (I think) I have to split the 2 words between "WHO WATCHES" and "THE WATCHERS" and then join them back up the other way around. Does that make sense? How would I do that? – lowlyf Apr 13 '13 at 23:20
  • Ah, you should edit your question to say that then. It also appears to be an exact duplicate of the question @NedDeily linked. – Emily Apr 13 '13 at 23:27

1 Answers1

6

Your code is reversing the order of the words, but not the actual words themselves. Try this:

def reverse_and_join(s):
  return "X".join(s[::-1].split(" "))

This code reverses the entire string, splits it by spaces, and then joins the reversed list of reversed words with "X". See this question for an explanation of the [::-1] syntax.

Community
  • 1
  • 1
Emily
  • 5,869
  • 1
  • 22
  • 15
  • You can simplify this by just reversing earlier. `"X".join(string[::-1].split())` – Gareth Latty Apr 13 '13 at 23:04
  • Thanks a lot. So if I were to put this in a function, would I say def a(words,blocksize) words = [word[::-1] for word in str.split(" ")] words.reverse() return "X".join(words) – lowlyf Apr 13 '13 at 23:08
  • I edited the answer to be a function that does this reverse-then-join thing to its argument and returns the result. I'm not sure what `blocksize` is supposed to be. – Emily Apr 13 '13 at 23:10
  • Except as mentioned earlier I have to split the 2 words between "WHO WATCHES" and "THE WATCHERS" and then join them back up the other way around as well. Does that make sense? How would I do that? – lowlyf Apr 13 '13 at 23:23
  • @Emily Why use 2 function - split and join - when replace will do the job? – volcano Apr 13 '13 at 23:41
  • @Lattyware It varies, So it could be the 3 word in the string, or it could be at the 5th word in the string. Test case would be >>> reverse_and_join('BIG BROTHER IS WATCHING', 1) # Test 1 'GIBXREHTORBXSIXGNIHCTAW' or it could be >>> reverse_and_join('BIG BROTHER IS WATCHING', 2) # Test 2 'SIXGNIHCTAWXREHTORBXGIB' – lowlyf Apr 13 '13 at 23:43
  • So the point at which to split is passed as an argument? You did not make that clear. – Gareth Latty Apr 13 '13 at 23:45
  • @Lattyware also could you please edit that last comment you made and remove the ATmyname. Thanks – lowlyf Apr 13 '13 at 23:47
  • @Lattyware I dont like having my name plastered all of the internet is all. I wasn't aware when I used my name to join that it would show in comments. – lowlyf Apr 13 '13 at 23:50
  • @Lattyware Yeah I have done that now, hence the "lowlyf" But the one you mentioned me in earlier hasnt updated. Would you be kind enough to change it to lowlyf please? :) thank you. – lowlyf Apr 13 '13 at 23:53