As a developer working on numerous projects for clients across the USA, I encountered a situation to swap characters in a string while processing and manipulating text data. Python provides several ways to accomplish this and in this article, we will explore how to swap characters in a string using Python in different approaches with examples and a screenshot of executed example code.
Swap Characters in a String Using Python
Let’s say you have a string variable called name that holds the value “John Smith”. Your task is to swap the first name and last name, so the resulting string becomes “Smith John”. We will solve this problem step by step using various Python techniques.
Read How to Insert a Python Variable into a String?
Approach 1: Use String Slicing
One of the simplest ways to swap string characters in Python is by using string slicing. Here’s how you can achieve it:
name = "John Smith"
swapped_name = name.split()[-1] + " " + name.split()[0]
print(swapped_name)Output:
Smith JohnI have executed the above example code and added the screenshot below.

Explanation:
- We start by splitting the
namestring into a list of substrings using thesplit()method. By default, it splits on whitespace, separating the first name and last name. - We access the last element of the split list using
[-1], which gives us the last name (“Smith”). - We concatenate the last name with a space and the first element of the split list (
[0]), which represents the first name (“John”). - Finally, we print the
swapped_namevariable, which contains the swapped name.
Check out How to Split a String into Equal Parts in Python?
Approach 2: Use List Manipulation
Another approach involves converting the string to a list, swapping the elements, and then joining them back into a string. Here’s an example:
name = "Emily Johnson"
name_list = name.split()
name_list[0], name_list[1] = name_list[1], name_list[0]
swapped_name = " ".join(name_list)
print(swapped_name)Output:
Johnson EmilyI have executed the above example code and added the screenshot below.

Explanation:
- We split the
namestring into a list calledname_listusing thesplit()method. - We swap the first and second elements
name_listusing the Python idiomname_list[0], name_list[1] = name_list[1], name_list[0]. This swaps the elements in a single line. - We join the elements
name_listback into a string using thejoin()method, with a space as the separator. - Finally, we print the
swapped_namevariable containing the swapped name.
Read Convert Int to Bytes in Python
Approach 3: Use String Replacement
If you want to swap specific characters within a string, you can use the replace() method. Here’s an example:
name = "Jenifer Davis"
swapped_name = name.replace("J", "_").replace("D", "J").replace("_", "D")
print(swapped_name)Output:
Denifer JavisI have executed the above example code and added the screenshot below.

Explanation:
- We start with the
namestring “Jenifer Davis”. - We use the
replace()method to replace all occurrences of “J” with “D”. - We then use
replace()again to replace the first occurrence of “D” with “J”. The second argument1specifies that only the first occurrence should be replaced. - Finally, we print the
swapped_namevariable, which contains the name with swapped characters.
Check out Convert DateTime to UNIX Timestamp in Python
Conclusion
In this tutorial, I have explained how to swap characters in a string using Python. I discussed a problem statement and approaches to solve that like using string slicing, list manipulation, and string manipulation.
You may also like to read:
- Concatenate String and Float in Python
- How to Find the Largest and Smallest Numbers in Python?
- How to Check if a Python String Contains a Substring?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.