Creating a New String from the First and Last Characters in Python
In this blog post, we’ll explore a simple Python program that creates a new string made up of the first 2 and last 2 characters of a given input string. This is a great exercise for beginners to practice string manipulation and looping in Python. We’ll break it down step-by-step to make it easy to understand.
The Problem
Our goal is to take a string input from the user and create a new string that consists of the first two and last two characters of the original string. If the input string is too short (less than 2 characters), we’ll simply return the original string.
Steps to Solve the Problem
- Take Input from the User: First, we’ll prompt the user to enter a string.
- Count the Characters: We’ll count the number of characters in the input string using a loop.
- Form the New String: Depending on the length of the string, we’ll either create a new string using slicing or return the original string.
- Print the Result: Finally, we’ll print the newly formed string.
Python Code
Here’s the Python code that accomplishes this:
# Step 1: Take input from the user
user_input = input("Enter the string: ")
# Step 2: Initialize a count variable
count = 0
# Step 3: Use a loop to count the number of characters in the string
for _ in user_input:
count += 1
# Step 4: Form the new string based on the length of the original string
if count > 2:
new_string = user_input[:2] + user_input[-2:]
else:
new_string = user_input # If the string is too short, return the original string
# Step 5: Print the newly formed string
print("New string:", new_string)
Explanation of the Code
- Input:
- We prompt the user to enter a string with
user_input = input("Enter the string: ")
.
- Counting Characters:
- We initialize a
count
variable to0
and use afor
loop to go through each character in the string. For each character, we incrementcount
by1
.
- Forming the New String:
- We check if the string has more than 2 characters using
if count > 2
. - If it does, we form a new string using
user_input[:2] + user_input[-2:]
. Here,[:2]
gets the first 2 characters, and[-2:]
gets the last 2 characters. - If the string has 2 or fewer characters, we just return the original string.
- Output:
- The program prints the new string using
print("New string:", new_string)
.
Example Test Cases
Let’s test our program with some example inputs:
- Example 1:
- Input:
"Python"
- Output:
"Pyon"
- Explanation: The first 2 characters are
"Py"
, and the last 2 characters are"on"
.
- Example 2:
- Input:
"abc"
- Output:
"abcc"
- Explanation: The first 2 characters are
"ab"
, and the last 2 characters are"bc"
.
- Example 3:
- Input:
"Hi"
- Output:
"Hi"
- Explanation: The string has exactly 2 characters, so the result is the original string.
- Example 4:
- Input:
"A"
- Output:
"A"
- Explanation: The string is shorter than 2 characters, so we return the original string.
Conclusion
This simple program demonstrates how to use loops, string slicing, and conditional statements in Python. It’s a great way to practice basic programming concepts and get comfortable with manipulating strings.