- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to swap the First and the Last Character of a string
In this article, we will explore a Python program to swap the first and last character of a string. Swapping characters within a string can be a useful operation in various scenarios, such as data manipulation, text processing, or even string encryption. By swapping the first and last characters, we can transform the string and potentially change its meaning or representation.
We will dive into the details of solving this problem efficiently using Python. We will discuss the approach, provide a step-by-step algorithm, and implement the solution using Python code. Additionally, we will include test cases to validate the program's functionality.
Let's get started by understanding the problem requirements and constraints in the next section.
Understanding the Problem
Before we dive into solving the problem, let's define the requirements and constraints more explicitly.
Problem Statement − We need to write a Python program that swaps the first and last character of a given string and returns the modified string.
Input − The program should take a string as input.
Output − The program should return the modified string with the first and last characters swapped.
The input string will have at least two characters.
The input string may contain any printable characters, including spaces and punctuation.
Now that we have a clear understanding of the problem, let's discuss our approach and the algorithm we will use to solve it in the next section.
Approach and Algorithm
To swap the first and last character of a string, we can follow a simple approach that involves the following steps −
Retrieve the first character of the string.
Retrieve the last character of the string.
Create a new string by replacing the first character with the last character and vice versa.
Return the modified string as the output.
Here's a step-by-step algorithm to illustrate the approach −
Read the input string.
Store the first character of the string in a variable, let's call it first_char.
Store the last character of the string in a variable, let's call it last_char.
Create a new string by concatenating last_char with the substring from the second character to the second-to-last character of the original string, and then concatenating first_char at the end.
Return the modified string as the output.
This approach ensures that the first and last characters of the string are swapped while maintaining the order of the remaining characters.
Now that we have discussed the approach and algorithm, let's move on to the implementation of the Python program in the next section.
Now, let's implement the Python program to swap the first and last character of a string. We'll follow the approach and algorithm discussed earlier.
def swap_first_last_character(string): first_char = string last_char = string[-1] modified_string = last_char + string[1:-1] + first_char return modified_string
In the above code, we define a function swap_first_last_character that takes a string as input. Inside the function, we extract the first character using string and the last character using string[-1]. Then, we create a new string modified_string by concatenating the last character, the substring from the second character to the second-to-last character of the original string (string[1:-1]), and finally, the first character. Finally, we return the modified string as the output.
Let's test the function with a few sample inputs to validate its functionality.
input_string = "Hello" output_string = swap_first_last_character(input_string) print(output_string)
input_string = "Python" output_string = swap_first_last_character(input_string) print(output_string)
input_string = "OpenAI" output_string = swap_first_last_character(input_string) print(output_string)
The program successfully swaps the first and last character of each input string, as expected.
In this article, we have explored how to swap the first and last character of a string using a Python program. We started by understanding the problem and the requirements, followed by discussing an approach and algorithm to solve it. Then, we implemented the program and tested it with sample inputs to ensure its correctness.
By swapping the first and last character of a string, we can transform the string and achieve various effects depending on the context. This operation can be useful in tasks such as data manipulation, text processing, or string encryption.
Kickstart Your Career
Get certified by completing the courseGet Started