Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String

Strings in Python:

“String is a character collection or array”

Well in Python too, for the string data type, we say the same definition. The string is a sequenced character array and is written within single, double, or three quotes. Also, Python does not have the data type character, thus it is used as a string of length 1 if we write ‘r’.

Given a string, the task is to form a new string which is made of the First 2 and Last 2 characters From a Given String in Python.

Examples:

Example1:

Input:

given string =BTechGeeks

Output:

BTks

Example2:

Input:

given string =AplusTopper

Output:

Aper

Program to Form a New String Made of the First 2 and Last 2 characters From a Given String

Below is the full approach to form a new string which is made of the First 2 and Last 2 characters From a Given String in Python.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

1)Using indexing, Slicing, and Count Variable(Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a variable to say stringcharacters that stores the total characters in the given string.
  • Initialize the stringcharacters to 0.
  • Traverse through the given string using For loop.
  • Increment the value of stringcharacters by 1.
  • Using String Concatenation and slicing form the new string with which is made of the First 2 and Last 2 characters From a Given String.
  • Print the modified string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
given_strng = 'BTechGeeks'
# Take a variable to say stringcharacters that stores the total characters in the given string.
# Initialize the stringcharacters to 0.
stringcharacters = 0
# Traverse the given string using for loop.
for charact in given_strng:
    # Increment the value of stringcharacters by 1.
    stringcharacters = stringcharacters+1
# Using String Concatenation and slicing form the new string with which is made of the First two
# and Last two characters From a Given String.
resstring = given_strng[0:2]+given_strng[stringcharacters-2:stringcharacters]
# print the result string
print(resstring)

Output:

BTks

2)Using indexing, Slicing, and Count Variable(User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a variable to say stringcharacters that stores the total characters in the given string.
  • Initialize the stringcharacters to 0.
  • Traverse through the given string using For loop.
  • Increment the value of stringcharacters by 1.
  • Using String Concatenation and slicing form the new string with which is made of the First 2 and Last 2 characters From a Given String.
  • Print the modified string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
given_strng = input('Enter some random string = ')
# Take a variable to say stringcharacters that stores the total characters in the given string.
# Initialize the stringcharacters to 0.
stringcharacters = 0
# Traverse the given string using for loop.
for charact in given_strng:
    # Increment the value of stringcharacters by 1.
    stringcharacters = stringcharacters+1
# Using String Concatenation and slicing form the new string with which is made of the First two
# and Last two characters From a Given String.
resstring = given_strng[0:2]+given_strng[stringcharacters-2:stringcharacters]
# print the result string
print(resstring)

Output:

Enter some random string = AplusTopper
Aper

Explanation:

  • A string must be entered by the user and saved in a variable.
  • The count variable is set to zero.
  • The for loop is used to go over the characters in a string.
  • When a character is encountered, the count is increased.
  • String slicing is used to create the new string, and the first and last two characters of the string are concatenated using the ‘+’ operator.
  • The string that has just been formed is printed.

3)Using indexing, Slicing, and len() function(Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Calculate the length of the string using the len() function and store it in a variable.
  • Using String Concatenation and slicing form the new string with which is made of the First 2 and Last 2 characters From a Given String.
  • Print the modified string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
given_strng = 'BTechGeeks'
# Calculate the length of the string using the len() function and store it in a variable.
stringcharacters = len(given_strng)
# Using String Concatenation and slicing form the new string with which is made of the First two
# and Last two characters From a Given String.
resstring = given_strng[0:2]+given_strng[stringcharacters-2:stringcharacters]
# print the result string
print(resstring)

Output:

BTks

Here string concatenation is used to combine the characters using string slicing and length of the string.

4)Using indexing, Slicing, and len() function(User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Calculate the length of the string using the len() function and store it in a variable.
  • Using String Concatenation and slicing form the new string with which is made of the First 2 and Last 2 characters From a Given String.
  • Print the modified string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
given_strng = input('Enter some random string = ')
# Calculate the length of the string using the len() function and store it in a variable.
stringcharacters = len(given_strng)
# Using String Concatenation and slicing form the new string with which is made of the First two
# and Last two characters From a Given String.
resstring = given_strng[0:2]+given_strng[stringcharacters-2:stringcharacters]
# print the result string
print(resstring)

Output:

Enter some random string = AplusTopper
Aper

Here string concatenation is used to combine the characters using string slicing and length of the string.
Related Programs: