In this tutorial, we’ll learn how to make a palindrome in Python. A palindrome is a string that reads the same forward and backward. A common example of a palindrome is the word “madam”.
We will create a Python program that checks if a given input string is a palindrome, and if not, converts it into a palindrome by appending characters at the end of the string.
Step 1: Accepting an Input String
First, we need to accept an input string from the user. We can use the input() function to achieve this. Make sure to store the input in a variable.
1 |
input_string = input("Enter a string: ") |
Step 2: Checking if the Input String is a Palindrome
Next, we’ll check if the input string is already a palindrome. We can reverse the string and compare it with the original string. If they are equal, the input string is a palindrome.
1 2 3 4 5 6 |
def is_palindrome(string): return string == string[::-1] input_string = input("Enter a string: ") if is_palindrome(input_string): print("The input string is already a palindrome") |
Step 3: Converting the Input String to a Palindrome
If the input string is not a palindrome, our program needs to add the required characters to make it into one. We will iterate through the characters of the string starting from the first character.
In each iteration, we will remove one character from the original string and check if it is a palindrome. If it is, we can append the removed characters in reverse order to the original string to get a palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def make_palindrome(string): removed_chars = "" for i in range(len(string)): temp_string = string[i:] if is_palindrome(temp_string): return string + removed_chars[::-1] else: removed_chars += string[i] input_string = input("Enter a string: ") if is_palindrome(input_string): print("The input string is already a palindrome") else: palindrome = make_palindrome(input_string) print("Palindrome string: ", palindrome) |
Step 4: Testing the Program
Now, we are ready to test our palindrome-making Python program with different input strings.
1 2 3 4 5 6 7 |
python make_palindrome.py Enter a string: racecar The input string is already a palindrome Enter a string: abc Palindrome string: abcba Enter a string: test Palindrome string: testset |
Full Code
Here’s the complete code for our Python program to make a palindrome:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def is_palindrome(string): return string == string[::-1] def make_palindrome(string): removed_chars = "" for i in range(len(string)): temp_string = string[i:] if is_palindrome(temp_string): return string + removed_chars[::-1] else: removed_chars += string[i] input_string = input("Enter a string: ") if is_palindrome(input_string): print("The input string is already a palindrome") else: palindrome = make_palindrome(input_string) print("Palindrome string: ", palindrome) |
Conclusion
Now you know how to make a palindrome in Python. By following this tutorial, you can create a program that accepts an input string, checks if it is a palindrome and if not, converts it into one by appending characters at the end of the string. You can further enhance this program by adding more features or optimizing the code. Happy coding!