-1
# Read an integer:
a = input()
#Now swap it...
a[0] = a[1]
a[1] = a[0]

As you can see I am trying to change the value and trying to swap it..

print(a)

...and then i print it out. But I am getting an error which is as follows:

Traceback (most recent call last):
  File "python", line 4, in <module>
TypeError: 'str' object does not support item assignment

For example, if my input is 79 I want the result to be 97. Can you tell me where my mistake is?

Cryptic Coder
  • 13
  • 1
  • 6

2 Answers2

1

Try this:

a = input()
a = str(a)
result = int(a[-1: : -1])
print(result)

Output: ( a = 34 )

43
Cosmos
  • 372
  • 2
  • 6
0

Based on your question simple thing you can do. As above comment string is not iterable while you as input. You need to convert to list to access by index. For swap you need to use temporary variable, so i used temp as variable to swap.

a = list(input())
#Now swap it...

print(a)
temp = a[0]
a[0] = a[1]

a[1] = temp

print(a)
print("".join(a))
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53