Open In App

Python – Create a string made of the first and last two characters from a given string

Improve
Improve
Like Article
Like
Save
Share
Report

Here we are going to see the approach of forming a string made from the first and last 2 characters of a given string.

Input: Geeksforgeeks
Output: Geks

Input: Hi, There
Output: Hire

Method #1: Using list slicing

In this example, we are going to loop through the string and store the length of the string in the count variable and then make the new substring by taking the first 2 characters and the last two characters with the help of the count variable.

Python




# Taking input from the user
inputString = "Geeksforgeeks"
 
count = 0
 
# Loop through the string
for i in inputString:
      count = count + 1
newString = inputString[ 0:2 ] + inputString [count - 2: count ]
 
# Printing the new String
print("Input string = " + inputString)
print("New String = "+ newString)


Output

Input string = Geeksforgeeks
New String = Geks

Time Complexity: O(n)
Auxiliary Space: O(n)

Methods #2: Using a loop

In this example we are going to store the length of the string in a variable and break the loop if its length is less than 4 characters otherwise we will store the characters if the variable matches the defined conditions and make a new string out of it.

Python




# Taking input from user
inputString = "Geeksforgeeks"
 
l = len(inputString)
newString = ""
 
# looping through the string
for i in range(0, len(inputString)):
    if l < 3:
        break
    else:
        if i in (0, 1, l-2, l-1):
            newString = newString + inputString[i]
        else:
            continue
 
# Printing New String
print("Input string : " + inputString)
print("New String : " + newString)


Output

Input string : Geeksforgeeks
New String : Geks

Time Complexity: O(n)
Auxiliary Space: O(n)

Approach 3: Using formatted string
 

Algorithm to create a string made of the first and last two characters from a given string using formatted string approach:

  1. Initialize a variable named “inputString” and assign a string to it as input.
  2. Create a formatted string by slicing the first two and last two characters from the “inputString” variable and concatenating them using “{}{}” format.
  3. Store the formatted string in a new variable named “newString”.
    Print the “inputString” and “newString” variables.

Python3




#Taking input from the user
inputString = "Geeksforgeeks"
 
#Creating formatted string
newString = "{}{}".format(inputString[0:2], inputString[-2:])
 
#Printing the new String
print("Input string = " + inputString)
print("New String = " + newString)


Output

Input string = Geeksforgeeks
New String = Geks

Time complexity:
The time complexity of the code is O(1) because it takes constant time to slice the input string and format the output string.

Auxiliary space:
The space complexity of the code is O(1) because it uses a constant amount of extra memory to store the input and output strings.



Last Updated : 24 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads