swap digits in a number in python

Python Program to Swap Two Digits in a Number

In this tutorial, we will look at how to swap two digits in a number in Python with the help of some examples.

If you prefer video over text, check out the following video detailing the steps in this tutorial –

Steps to swap digits in a number

You can use the following steps to swap two digits in a number in Python –

  1. Convert the number to a string.
  2. Create a list from the string – This will result in a list of character digits.
  3. Swap the required digits in the list using their respective indices.
  4. Join the list to get a string.
  5. Convert the string from step 4 to a number.

Examples

Let’s now look at some examples of using the above method.

Let’s say we have the number 1234 and we want to swap the digits 2 and 4 in this number such that the resulting number becomes 1432.

num = 1234
# convert number to string
s = str(num)
# create list from string
ls = list(s)
# swap the characters in the list, for example, let's swap the digits 2 and 4 which are at indices 1 and 3 respectively
ls[1], ls[3] = ls[3], ls[1]
# join the list elements in a string
new_s = "".join(ls)
# convert string to integer
new_num = int(new_s)

print(new_num)

Output:

1432

You can see that we get 1432 as the result.

Note This method swaps the digits based on their position and thus will also work if there are duplicate digits present as well. We simply need to identify the position of the two elements two be swapped in the list.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

Let’s say you have the number 12324 and you want to swap the first 2 with the digit 4 such that the result is 14322. In that case, we can use the above method and specify the index of the elements to be swapped in the list as 1 (for the first 2) and 4 (for the element 4).

num = 12324
# convert number to string
s = str(num)
# create list from string
ls = list(s)
# swap the characters in the list, for example, let's swap the first occurrence of 2 with the digit 4
ls[1], ls[4] = ls[4], ls[1]
# join the list elements in a string
new_s = "".join(ls)
# convert string to integer
new_num = int(new_s)

print(new_num)

Output:

14322

We get 14322 as the result.

Swapping v/s Replacing

Note that performing a swap operation is different from performing a replace operation.

  • In a swap operation, both the digits to be swapped are present in the number, we’re just exchanging their positions.
  • In a replace operation, we replace one or more occurrences of a digit with another digit (that may or may not be present in the number).

In an example above, for the number 1234 we got the result of swapping 2 and 4 as 1432. If you instead apply the replace operation such that you replace 2 with 4, we would get 1434 as the output.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top