user3721227
user3721227

Reputation: 23

Trying to break a string up and reverse it

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( );

Upvotes: 0

Views: 141

Answers (7)

Eli Rose
Eli Rose

Reputation: 7008

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)

Upvotes: 1

Pushkar Newaskar
Pushkar Newaskar

Reputation: 169

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])

Upvotes: 1

Nabin
Nabin

Reputation: 11776

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.

Upvotes: 1

Ryan Haining
Ryan Haining

Reputation: 36842

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]

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304335

>>> s = 'first middle last'
>>> L = s.split()
>>> L[0] = L[0][::-1]
>>> L
['tsrif', 'middle', 'last']
>>> print L[::-1]
['last', 'middle', 'tsrif']

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49310

>>> my_string = raw_input('?: ').split()
?: first middle last
>>> new_string = ' '.join(my_string[:0:-1] + [my_string[0][::-1]])
>>> new_string
'last middle tsrif'

Upvotes: 1

vk1011
vk1011

Reputation: 7179

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'

Upvotes: 0

Related Questions