How to swap two characters in a string in python?

In this article, we will discuss how to swap two characters in a string. There are various approaches that will help to understand how we can swap two characters in a string.

Approach 1: Using list() and join() Method

If we want to swap two characters in a string, we can use the list() and join() method.

Example:

Output:

hewlo lorld

Explanation:

In the code above, we first define the input string as "hello world". After that, we convert the string to a list of characters using the list() method. Next, we use tuple unpacking to swap the characters at index 2 and index 6 in the list. Finally, we convert the list back to a string using the join() method and output the new string

Approach 2: Using slicing and concatenation

If we want to swap two characters in a string, we can use the slicing and concatenation method.

Example:

Output:

hewlo lorld

Explanaton:

In this approach, we use string slicing and concatenation to swap the characters. We slice the string into four parts: the substring before the first character to be swapped (string[:2]), the first swapped character (string[6]), the substring between the swapped characters (string[3:6]), the second swapped character (string[2]), and the substring after the second swapped character (string[7:]). After that, we concatenate these substrings in the desired order to create the new string.

Approach 3: Using a list comprehension

If we want to swap two characters in a string, we can use the list comprehension method.

Example:

Output:

hewlo lorld

Explanation:

In this approach, we create a list of characters using a list comprehension that iterates over the indices of the input string. We use a conditional expression within the list comprehension to swap the characters at index 2 and index 6.

The expression string[6 if i == 2 else 2] returns the character at index 6 if the current index is 2, and returns the character at index 2 if the current index is 6. Otherwise, it returns the original character at the current index. After that, we convert the list of characters back to a string using the join() method and output the new string.

This approach may be less efficient than the previous approaches because it creates a new list of characters, but it may be useful in situations where a list comprehension is more convenient or intuitive to use.

Approach 4: Using 'bytearray' built-in type

If we want to swap two characters in a string, we can use the bytearray method.

Example:

Output:

hewlo lorld

Explanation:

In this approach, we first convert the input string to a bytearray using the bytearray() built-in function. After that, we specify the 'utf-8' encoding to convert the string to byte, and then we swap the two characters at index 2 and index 6 by directly accessing the corresponding elements in the bytearray. Finally, we convert the bytearray back to a string using the decode() method and output the new string.

This approach is efficient because it uses a mutable bytearray instead of creating a new string or list.