Trying to break a string up and reverse it

191 Views Asked by At

I am trying to code a program which will ask for a person's full name (first, middle, last) and will print (last, middle, tsrif). Like print their name backwards, but the letters are backwards only on their first name. I can't figure out how to flip the order of the words without flipping their letters. Any help?

My code so far:

import sys
str = raw_input("first middle last")

str.split( );
7

There are 7 best solutions below

0
On

The str.split() function returns the list, it doesn't modify it in place. So you want :

split_up = str.split()

(obviously split_up is an arbitrary name, the point is that it's a list)

0
On
input_name = "first middle last" # whatever your input is: using as example here
split_name = input_name.split()
desired_output = [split_name [2], split_name [1], split_name [0][::-1]]

>>> desired_output
 ['last', 'middle', 'tsrif']

>>> ' '.join(desired_output)
'last middle tsrif'

>>> ', '.join(desired_output)
'last, middle, tsrif'
0
On
>>> my_string = raw_input('?: ').split()
?: first middle last
>>> new_string = ' '.join(my_string[:0:-1] + [my_string[0][::-1]])
>>> new_string
'last middle tsrif'
0
On
>>> s = 'first middle last'
>>> L = s.split()
>>> L[0] = L[0][::-1]
>>> L
['tsrif', 'middle', 'last']
>>> print L[::-1]
['last', 'middle', 'tsrif']
0
On

Let's assume you have a variable s which you've gotten from the user. If you want to split that up into individual words, you use .split() and save the result

s = "john henry smith"
parts = s.split() # ['john', 'henry', 'smith']

now, to reverse these words, you can use the built in function reversed and convert the result to a list

rev_parts = list(reversed(parts))  # ['smith', 'henry', 'john']

Finally you want to reverse just the last element of rev_parts, you can use a -1 index to indicate the last element in the list. What you want to do is overwrite the existing element. We can use reversed again here, but we'll also need to use a join to tell it to but all the characters back together after

rev_name = ''.join(reversed(rev_parts[-1])) # 'nhoj'
rev_parts[-1] = rev_name # overwrite 'john'

Now you have a list of what you want. If you want to put them all back together separated by spaces, use another join

result = ' '.join(rev_parts)

You can eliminate various intermediate variables and steps here as you feel comfortable.


An alternative to using reversed is to use the slice syntax [::-1] which says, from the end to the beginning, back one character at a time. In that case you'd have

rev_parts = parts[::-1]
rev_parts[-1] = rev_parts[-1][::-1]
0
On

Reverse the list at first. Then reverse the last string in list

my_list = string.split()
my_list = list(reversed(my_list))
my_list[2] = my_list[2][::-1]
print my_list

Don't use str as variable.

0
On

This should work

import sys
str = "first middle last";
allWords = str.split(" ");
length = len(allWords)
for index in range(length-1):
    print(allWords[length-index-1],end=" ")
print((allWords[0])[::-1])