Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reverse word
#1
Hi!
I need to reverse words like this:
cake
python
Output:
keca onthpy
I used [::-1] but this isn't a correct answer because it gives me:
cake
python
Output:
ekac nohtyp
Any suggestion?
Reply
#2
there is for sure much cleaner way but for now :) :
n = 2    # num of chars 
s = ''.join([line[i:i+n] for i in range(0, len(line), n)][::-1])
with line being your string.
First you create a list out of every n number of characters, then you reverse the list and join the elements into string again.

cake:
Output:
'keca'
python:
Output:
'onthpy'
Reply
#3
You should specify your task more precisely. It's not obvious what are the exact requirements. It seems that there should be two letter chunks from word and they should be in reverse order.

cake -- > ca ke --> ke ca
python --> py th on --> on th py


Do words have always even number of characters?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Jan-28-2019, 09:15 AM)mlieqo Wrote: there is for sure much cleaner way but for now :) :
n = 2    # num of chars 
s = ''.join([line[i:i+n] for i in range(0, len(line), n)][::-1])
with line being your string.
First you create a list out of every n number of characters, then you reverse the list and join the elements into string again.

cake:
Output:
'keca'
python:
Output:
'onthpy'

THANKS! This works!
Reply
#5
There is built-in module textwrap which can be used to get desired results:

>>> from textwrap import wrap
>>> a = 'python'
>>> ''.join(reversed(wrap(a, width=2)))         # [::-1] can be used instead of reversed
onthpy
Just for fun: inefficient mental exercise :

>>> a = 'python'
>>> ''.join([a[a.index(x):a.index(x) + 2] for x in a][::2][::-1])
onthpy
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Cleaner

print(string[::-1])
Reply
#7
(Jul-02-2020, 04:00 PM)chesschaser Wrote: Cleaner

print(string[::-1])
This doesn't work for him like he already said, @chesschaser
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Python Speech recognition, word by word AceScottie 6 15,860 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Reverse the string word sneha 2 2,568 Dec-12-2019, 03:37 AM
Last Post: sneha
  print a word after specific word search evilcode1 8 4,721 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  difference between word: and word[:] in for loop zowhair 2 3,626 Mar-03-2018, 07:24 AM
Last Post: zowhair

Forum Jump:

User Panel Messages

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