-2

For instance, myList = (['string21', 'string43', 'string65'])... now I want my result to be this (without [ ] and , ):

 '12gnirts 34gnirts 56gnirts'

I've found few examples on how to reverse strings in general but not specific to this problem ... I'm bit confused on how to do a reverse of strings that are in a list.

Below is my code:

reverseEachCharacter = [x[::-1] for x in myList]
print "Result: ", reverseEachCharacter
# prints this:['1gnirts', '2gnirts', '3gnirts']
John Doe
  • 53
  • 4
  • 14
  • You need another ' after string65. And why is your result missing a few numbers? – dfundako Feb 26 '18 at 20:41
  • 3
    What is the difference to [these question and answers of yours](https://stackoverflow.com/q/48952467/8881141) from three days ago? – Mr. T Feb 26 '18 at 20:41
  • You just copied and pasted the answer from older question here. Please try to search and play with your code, it is for your own good. – Moinuddin Quadri Feb 26 '18 at 20:56

1 Answers1

3

You are currently printing the list. [...] is because of it being the list. Just use " ".join(reverseEachCharacter) to join your strings in the list to create another string as:

myList = ['string21', 'string43', 'string65']
reverseEachCharacter = [x[::-1] for x in myList]
print (repr(" ".join(reverseEachCharacter)))

Output:

'12gnirts 34gnirts 56gnirts'

Edit: Found that repr is made just for printing with the quotes! Though "'{}'".format() will be still better for the flexibility.

Udayraj Deshmukh
  • 1,814
  • 1
  • 14
  • 22
  • 2
    I can show you how to do that, too. But why don't you try to figure it out by yourself? I see that the code you posted in question itself was from an answer to your previous question. Try to give effort from your side first :) – Udayraj Deshmukh Feb 26 '18 at 20:47
  • absolutely! but the answer you gave to the question is also wrong is what I'm saying... I can send you my tries... which I couldn't put up here because of the max length... here are my few tries: `addCommas = (" ' ".join(reverseEachCharacter)); print "My last reversed strings:",(addCommas)` – John Doe Feb 26 '18 at 20:59
  • There you have my way of doing it :D – Udayraj Deshmukh Feb 26 '18 at 21:06
  • You're welcome. If you've found what you want from this answer, please mark it as accepted. – Udayraj Deshmukh Feb 26 '18 at 21:24