Swapping characters in string inputted from user in python
<p>I'm a beginner in Python. I have already taken an input from the user for string and turned it into a list so I can manipulate the individual characters. I know how to switch say, the first and last letter, etc. but I am confused on what code to use because I don't know how many characters the user will input. Someone told me to think about it in terms of l[0] with the end of len(l), l[1] with len(l)-1,... but there are n number of characters I am dealing with. Do I need to use a loop? Thank you.</p>

<p>In python, reversing the order of a list is as simple as <code>list[::-1]</code></p>

<p>So say</p>

<pre><code>a='my_string'
a=a[::-1]
print(a)
</code></pre>

<p>Your output will be </p>

<pre><code>gnirts_ym
</code></pre>

<p>For more information on how this works, have a look at <a href="http://pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/" rel="nofollow">slicing in python</a>. Note, however, that strings in python are immutable, meaning you cannot change a portion of them using slicing (for instance, <code>a[2:4]='yo'</code> ).</p>

python string python-3.x