Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 16 Aug, 2022
Improve Article
Save Article

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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!