How to Swap First and Last Characters from Input in Python

Question:

I am brand new to programming, learning Python3 from an online course. This exercise asks me to write a program which reads a string using input(), and outputs the same string but with the first and last character exchanged (example: Fairy becomes yairF). There is likely a simpler way to do this with more advanced functions, however, for the purposes of this exercise, I am supposed to write the program using only classes, substrings and indices. Here is what I have now:

myString = input()
newString = (myString[1:len(myString)-1])
print(myString[len(myString)-1],newString,myString[0])

Using the example of input ‘Fairy’, this outputs ‘y air F’, which is not what I’m looking for. I also tried

myString = input()
newString = (myString[1:len(myString)-1])
newString[0] = myString[len(myString)-1]
newString[len(newString)-1] = myString[0]
print(newString)

However, line three gave me: TypeError: 'str' object does not support item assignment. So I’m guessing I can’t assign new values to places in an index. My other approach, below, also gave me an error:

myString = input()
newString = (myString[1:len(myString)-1])
lastString = str(myString[len(myString)-1],newString,myString[0])
print(lastString)

Line three gave me: TypeError: decoding str is not supported, so it seems that I can’t combine them that way. Any tips on handling this?

Asked By: user2498805

||

Answers:

Try this:

>>> temp = "abcde"
>>> temp[1:-1]
'bcd'
>>> temp[-1:] + temp[1:-1] + temp[:1]
'ebcda'
>>> 

In short, python has magnificent string slicing syntax. You can do magic with it.

Answered By: ducin

You can simply do the following:

myString = input()
newString = (myString[1:len(myString)-1])
print(myString[len(myString)-1]+newString+myString[0])

Done!

Answered By: user2464953

Just for fun:

>>> temp = "abcde"
>>> temp2 = list(temp)
>>> temp2[0],temp2[-1]=temp2[-1],temp2[0]
>>> temp = ''.join(temp2)
>>> temp
'ebcda'
Answered By: Benjamin Toueg

Strings in python are immutable, so you can’t simply do this:

mystr[0] = mystr[-1]

To swap letters in a string, you can make it into a list then simply change the order of the list:

mystr = "abcde"
mylist = list(mystr)
store = mylist[0], mylist[-1]
mylist[0] = store[1]
mylist[-1] = store[0]
print ''.join(mylist)

Prints:

ebcda
Answered By: TerryA

This will work for temp = 'x' as well.

temp[-1] + temp[1:-1] + temp[0] if temp[1:-1] else temp[::-1]
Answered By: dansalmo
word = input()
wordLength = len(word)
print(word[wordLength-1]+word[1:wordLength-1]+word[wordLength-wordLength])
Answered By: Charles Ludwig

I stumbled upon to this question too, and after reading the answers I found probably the easiest way to do it:

mystring= input(cantastif) 

print(mystring[-1]+mystring[1:8]+mystring[0]) 

I deliberately used an example as I think it is easier to understand

Answered By: Sofienne Beau
string = input()

slice_mid = string[1:-1]
slice_end = string[-1:]
slice_beg = string[0]

print (slice_end + slice_mid + slice_beg)

This is what my noob brain came up with.

Answered By: Just Passing Thru
def front_back(word):
    word_list = list(word)
    keep_in_mind = word_list[0], word_list[-1]
    word_list[0] = keep_in_mind[1]
    word_list[-1] = keep_in_mind[0]
    return (''.join(word_list))

print(front_back('a'))
Answered By: fLoveARTh
word = input()
print(word[-1]+word[1:-1]+word[0])

I had trouble with this one – found this explanation online which helped

  1. We initialize a variable start, which stores the first character of the string (string[0])
  2. We initialize another variable end that stores the last character (string[-1])
  3. Then we will use string slicing, string[1:-1], this will access all the characters from the 2nd position excluding the last character.
  4. Then we add these three as required forming a new string that has the first and last characters of the original string swapped. And then we will print it.

I had originally added additional variables as placeholders for the first and last character of the string but you can do it only by calling the indexes, slicing, and concatenating the strings.

Answered By: pdxpjh

the below solution can work too as I have tested it.

word = input()

x = list(word)

y = x[0]

x[0] = x[-1]

x[-1] = y

print("".join(x))

thank you.

Answered By: Fahad Ali
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.