How to Swap Characters in a String Using Python?

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 John

I have executed the above example code and added the screenshot below.

Swap Characters in a String Using Python

Explanation:

  1. We start by splitting the name string into a list of substrings using the split() method. By default, it splits on whitespace, separating the first name and last name.
  2. We access the last element of the split list using [-1] , which gives us the last name (“Smith”).
  3. We concatenate the last name with a space and the first element of the split list ([0]), which represents the first name (“John”).
  4. Finally, we print the swapped_name variable, 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 Emily

I have executed the above example code and added the screenshot below.

How to Swap Characters in a String Using Python

Explanation:

  1. We split the name string into a list called name_list using the split() method.
  2. We swap the first and second elements name_list using the Python idiom name_list[0], name_list[1] = name_list[1], name_list[0]. This swaps the elements in a single line.
  3. We join the elements name_list back into a string using the join() method, with a space as the separator.
  4. Finally, we print the swapped_name variable 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 Javis

I have executed the above example code and added the screenshot below.

Swap Characters in a String Using Python string replacement

Explanation:

  1. We start with the name string “Jenifer Davis”.
  2. We use the replace() method to replace all occurrences of “J” with “D”.
  3. We then use replace() again to replace the first occurrence of “D” with “J”. The second argument 1 specifies that only the first occurrence should be replaced.
  4. Finally, we print the swapped_name variable, 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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.