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.
Input string = Geeksforgeeks New String = Geks
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.
Input string : Geeksforgeeks New String : Geks
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.