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 e
and a
gets swapped with t
.
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
list()
function. - 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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# 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.
Examples
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)
Output:
ehta
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)
Output:
act
The last character didn’t get swapped.
FAQs
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.