Creating a New String From the First and Last Characters in Python

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

  1. Take Input from the User: First, we’ll prompt the user to enter a string.
  2. Count the Characters: We’ll count the number of characters in the input string using a loop.
  3. 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.
  4. 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

  1. Input:
  • We prompt the user to enter a string with user_input = input("Enter the string: ").
  1. Counting Characters:
  • We initialize a count variable to 0 and use a for loop to go through each character in the string. For each character, we increment count by 1.
  1. 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.
  1. 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:

  1. Example 1:
  • Input: "Python"
  • Output: "Pyon"
  • Explanation: The first 2 characters are "Py", and the last 2 characters are "on".
  1. Example 2:
  • Input: "abc"
  • Output: "abcc"
  • Explanation: The first 2 characters are "ab", and the last 2 characters are "bc".
  1. Example 3:
  • Input: "Hi"
  • Output: "Hi"
  • Explanation: The string has exactly 2 characters, so the result is the original string.
  1. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top