Thursday, December 26, 2024
Google search engine
HomeLanguagesPython Program to swap the First and the Last Character of a...

Python Program to swap the First and the Last Character of a string

Given a String. The task is to swap the first and the last character of the string.

Examples:

Input: GeeksForGeeks
Output: seeksForGeekG
 
Input: Python
Output: nythoP

Python string is immutable which means we cannot modify it directly. But Python has string slicing which makes it very easier to perform string operations and make modifications. Follow the below steps to swap characters –

  1. We initialize a variable start, which stores the first character of the string (string[0])
  2. We initialize another variable end that stores the last character (string[-1])
  3. Then we will use string slicing, string[1:-1], this will access all the characters from the 2nd position excluding the last character.
  4. Then we add these three as required forming a new string that has the first and last characters of the original string swapped. And then we will print it.

Below is the implementation of the above approach:

Python3




def swap(string):
     
    # storing the first character
    start = string[0]
     
    # storing the last character
    end = string[-1]
     
    swapped_string = end + string[1:-1] + start
    print(swapped_string)
     
# Driver Code
swap("Lazyroar")
swap("Python")


Output

seeksforGeekG
nythoP

Time Complexity: O(k), as string slicing takes O(k)
Auxiliary Space: O(n)

Method : Using lists without slicing

Python3




# swap first and last character
string="Lazyroar"
x=list(string)
temp=x[0]
x[0]=x[-1]
x[-1]=temp
print("".join(x))


Output

seeksforGeekG

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3: Using the built-in function join() and list comprehension.

Step-by-step approach:

  • Convert the given string into a list of characters using list comprehension.
  • Swap the first and last character by indexing the first and last elements and assigning them to the opposite positions.
  • Convert the list of characters back into a string using join().

Below is the implementation of the above approach:

Python3




def swap(string):
    # Convert the string to a list of characters
    char_list = [char for char in string]
     
    # Swap the first and last characters
    char_list[0], char_list[-1] = char_list[-1], char_list[0]
     
    # Convert the list of characters back to a string
    swapped_string = ''.join(char_list)
     
    print(swapped_string)
     
# Driver code
swap("Lazyroar")
swap("Python")


Output

seeksforGeekG
nythoP

Time complexity: O(n) where n is the length of the given string. This is because we need to iterate over each character of the string to convert it into a list of characters and then back to a string.
Auxiliary space: O(n) where n is the length of the given string. This is because we are creating a new list of characters to store the characters of the given string.

RELATED ARTICLES

Most Popular

Recent Comments