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 –

- Convert the number to a string.
- Create a list from the string – This will result in a list of character digits.
- Swap the required digits in the list using their respective indices.
- Join the list to get a string.
- 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.

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 –

- Swap Elements in a Python Tuple
- Swap Adjacent Characters in a Python String with this Method
- Easily Swap Keys and Values in a Python Dictionary with this method

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