# 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`.

Highlighted programs for you

Flatiron School

Flatiron School

Data Science Bootcamp
Product Design UX/UI Bootcamp

University of Maryland Global Campus

University of Maryland Global Campus

Cloud Computing Systems Master's
Digital Forensics & Cyber Investigation Master's

Creighton University

Creighton University

Health Informatics Master's

```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, ls = ls, ls
# 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, ls = ls, ls
# 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 –

• 