Python Forum
Reversing word in strings yields unexpected result
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reversing word in strings yields unexpected result
#1
Hello there, 
I'm recently learning python using python 3.6
I'm confused why the str.join seems not to work properly in my code
there are many examples I've read and I can't find the example using input()

def rev():
   my_input=input("Please input what you want to reverse: ")
   my_output=my_input.split()
   for word in my_output:
       my_output=" ".join(my_output[::-1])
   print(my_output)

rev()
If the input is "Python forum" the output should be "nohtyP murof". However it would yield "n o h t y P   m u r o f", and if I remove the space in the join, it would yield "nohtyPmurof". Help me, I'm stuck on this. Thank you. PS: if you would, please don't use a complex example like doing this whole task in one line, I really am a beginner.
Reply
#2
converting to a list, and back to a string is pointless as you can create a new string reversed
>>> s = "Python Forum"
>>> s[::-1]
'muroF nohtyP'
Quote:PS: if you would, please don't use a complex example like doing this whole task in one line, I really am a beginner.
The [::-1] is splicing. in which is [START:STOP:STEP] where any field can be empty. start and stop is empty which means the whole string, -1 for step reversing the whole string. More info here under indexing and splicing
https://python-forum.io/Thread-Basic-Strings

EDIT:
just realized your rev the words as well. Here is how
s = 'Python Forum'
rev_str = s[::-1]
rev_words = rev_str.split()[::-1]
print(' '.join(rev_words))
Recommended Tutorials:
Reply
#3
Yes, but it looks like he wants to keep the words in the same order, just reverse the letters.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
My clunky solution:

def rev():
    my_input = input("Please input what you want to reverse: ")
    my_output = my_input.split()
    new_list = []
    for word in my_output:
        new_list.append(word[::-1])
    reversy = " ".join(new_list)
    print(reversy)

rev()
Output:

Output:
C:\Python36\python.exe C:/Python/Gui/tkinter/raspi/scratch1.py Please input what you want to reverse: Python forum nohtyP murof Process finished with exit code 0
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
In [1]: output = ' '.join([word[::-1] for word in 'Python forum'.split()])

In [2]: output
Out[2]: 'nohtyP murof'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 758 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,758 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,488 Aug-12-2021, 04:25 PM
Last Post: palladium
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 1,918 Feb-10-2021, 11:06 AM
Last Post: Larz60+
  python --version yields no output, not detected by other programs ten 3 3,431 Jun-25-2020, 04:48 AM
Last Post: perfringo
  pillow reversing the order of pixels after every row johnEmScott 4 3,114 May-27-2020, 09:42 AM
Last Post: scidam
  Unexpected result linton 4 1,983 May-02-2020, 01:15 PM
Last Post: linton
  Python Speech recognition, word by word AceScottie 6 15,989 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  list sum gives unexpected result Nesso 0 1,687 Feb-04-2020, 08:31 AM
Last Post: Nesso
  Unexpected (?) result with regular expressions guraknugen 2 2,208 Jan-18-2020, 02:33 PM
Last Post: guraknugen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020