In this tutorial, we will look at how we can swap adjacent characters in a string. Let’s look at an example to better understand the task at hand.
Swapping adjacent characters example
Let’s say you have the string, “heat”, after swapping the adjacent characters, the string becomes “ehta”. Here, we’re swapping the ith character in the string with the (i+1)th character. That is,
h gets swapped with
a gets swapped with
What if there are an odd number of characters in the string?
In that case, the last character doesn’t get swapped. For example, swapping adjacent characters in the string “cat” should result in “act”. Since we didn’t have a following adjacent character for t, it remains as it is.
How to swap adjacent characters in a Python string?
Strings are immutable in Python, so once they are created, you cannot actually change the position of the characters inside a string. You can, however, create a new string with the required modifications.
To swap the adjacent characters in a string, use the following steps –
- Convert the string to a list (which is a mutable object in Python) of its characters using the
- Iterate through each pair of values starting from the left in the resulting list and swap them.
- Join the characters in the list together using the string
join()function to get the string that has the adjacent characters from the original string removed.
The following is a function that implements the steps mentioned above.
# function to swap the adjacent characters in a string and return the new string def swap_adjacent_chars(s): # create a list from string characters ls = list(s) # iterate through each pair of values in the list for i in range(0, len(ls)-1, 2): # swap the adjacent values ls[i], ls[i+1] = ls[i+1], ls[i] # join the characters in the list to get the result result = "".join(ls) return result
Here, we are using the
range() function in Python to iterate through each pair of values in a list – notice that we’re using a step size of 2.
Let’s now look at some examples of using the above syntax, we’ll apply the function created above directly on some strings.
First, let’s take an example where the string has an even number of characters.
# create a string s1 = "heat" # swap the adjacent characters res = swap_adjacent_chars(s1) # display the result print(res)
Note that you can reassign a variable (storing a string value) to a new value, so we could directly store the result from the
swap_adjacent_chars() function if the original string is not required.
Let’s now take an example where the string has an odd number of characters.
# create a string s2 = "cat" # swap the adjacent characters res = swap_adjacent_chars(s2) # display the result print(res)
The last character didn’t get swapped.
Convert the string to a list, iterate through each pair of values in the list from the left and swap them. Join the list elements back to get the resulting string.
In the string has an odd number of characters, then the last character in the string doesn’t get swapped.
You might also be interested in –
- Swap Two Words in a String with these Methods in Python
- Swap Characters in a Python String with this Method
- Swap Elements in a Python List with these Methods
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.