Python Program to Form a New String where the First Character and the Last Character have been Exchanged

Strings in Python:

A string is one of the most frequent data types in any computer language. A string is a collection of characters that can be used to represent usernames, blog posts, tweets, or any other text content in your code. You can make a string and assign it to a variable by doing something like this.

given_string='btechgeeks'

Strings are considered immutable in Python, once created, they cannot be modified. You may, however, construct new strings from existing strings using a variety of approaches. This form of programming effort is known as string manipulation.

Examples:

Example1:

Input:

given string =  hello this is btechgeeks

Output:

The original string before modification = hello this is btechgeeks
The new string after modification = sello this is btechgeekh

Example2:

Input:

given string = this is btechgeeks online coding platform for geeks

Output:

Enter some random string = this is btechgeeks online coding platform for geeks
The original string before modification = this is btechgeeks online coding platform for geeks
The new string after modification = shis is btechgeeks online coding platform for geekt

Program to Form a New String where the First Character and the Last Character have been Exchanged

Below are the ways to form a new string in which the first and last characters have been swapped

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

1)Using slicing(static input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a function that swaps the first and last character of the string.
  • Pass the given string as an argument to the function.
  • Split the string in the function.
  • The last character is then added to the middle half of the string, which is then added to the first character.
  • The modified string will be returned.
  • Print the modified string
  • The Exit of the program.

Below is the implementation :

# Take a function that swaps the first and last character of the string.
def swapString(given_string):
    # Split the string in the function.
    # The last character is then added to the middle half of the string,
    # which is then added to the first character.
    modifiedstring = given_string[-1:] + given_string[1:-1] + given_string[:1]
    # The modified string will be returned.
    return modifiedstring


# Give the string as static input and store it in a variable.
given_string = 'hello this is btechgeeks'
# printing the original string before modification
print('The original string before modification =', given_string)
# Pass the given string as an argument to the function.
modified_string = swapString(given_string)
# printing the new string after modification
print('The new string after modification =', modified_string)

Output:

The original string before modification = hello this is btechgeeks
The new string after modification = sello this is btechgeekh

Explanation:

  • Give the string as static input and store it in a variable.
  • This string is supplied to a function as an argument.
  • Using string slicing, the string is split into three parts in the function: the last character, the middle part, and the first character.
  • The ‘+’ operator is then used to concatenate these three parts.
  • After that, the modified string is printed.

2)Using slicing(User input)

Approach:

  • Give the string as user input using int(input()) and store it in a variable.
  • Take a function that swaps the first and last character of the string.
  • Pass the given string as an argument to the function.
  • Split the string in the function.
  • The last character is then added to the middle half of the string, which is then added to the first character.
  • The modified string will be returned.
  • Print the modified string
  • The Exit of the program.

Below is the implementation :

# Take a function that swaps the first and last character of the string.
def swapString(given_string):
    # Split the string in the function.
    # The last character is then added to the middle half of the string,
    # which is then added to the first character.
    modifiedstring = given_string[-1:] + given_string[1:-1] + given_string[:1]
    # The modified string will be returned.
    return modifiedstring


# Give the string as user input using int(input()) and store it in a variable.
given_string = input('Enter some random string = ')
# printing the original string before modification
print('The original string before modification =', given_string)
# Pass the given string as an argument to the function.
modified_string = swapString(given_string)
# printing the new string after modification
print('The new string after modification =', modified_string)

Output:

Enter some random string = this is btechgeeks online coding platform for geeks
The original string before modification = this is btechgeeks online coding platform for geeks
The new string after modification = shis is btechgeeks online coding platform for geekt

Related Programs: