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


This tutorial is all about string slicing. This involves creating a new string using the first and last two characters of a given string. Examples include using string slicing to extract the first two characters of a string. Such as "he" and "lo," and combining these slices to create a new string with the first and last two characters. String manipulation methods prove handy while working with large strings or performing specific operations on a string's individual components. Let us explore several ways to handle this.

Algorithm

  • Begin with the string that has been provided

  • Use slicing [:2] to extract the first two characters of the original string.

  • Use negative slicing to extract the final two characters from the string [-2:]

  • Form the result by concatenating the extracted first two characters and last two characters (new string = given string [:2] + given string [-2:])

String Slicing in Python

Use square bracket notation with the start and end indices separated by a colon to slice a string. The slice contains characters from the start index but not from the end index. Eg:

my_string = "Hello, Python!"
substring = my_string[2:7]
print(substring) # Output: "llo, "

Negative Indices

Python allows for negative indices. Now you can count characters from the string's end. Using negative indices in slicing makes it easier to retrieve substrings from the end of a string.

my_string = "Python is awesome"
substring = my_string[-7:-1]
print(substring) # Output: "awesom"

Step Size

Instead of just two indices, try adding three. The syntax is string[start:end:step]. It means how many characters to skip after each step.

my_string = "Hello, World!"
substring = my_string[1:10:2]
print(substring) # Output: "el,W"

Advanced Slicing

Specifies the start or end index to slice from the start or end of the string, accordingly. Moreover, negative step size allows for reverse slicing. Eg.

my_string = "TechCrunch"
substring1 = my_string[3:] # Output: "hCrunch"
substring2 = my_string[:6] # Output: "TechCr"
substring3 = my_string[::-1] # Output: "hcnurChet"

Example

Here’s how you make a new string using the first and last two characters:

new_string = given_string[:2] + given_string[-2:]
given_string = "TechCrunch"
new_string = given_string[:2] + given_string[-2:]
print(new_string)

Output

Tech

Example

given_string = "Python"
new_string = given_string[:2] + given_string[-2:]
print(new_string)

Output

Pyon

Example

Another Example to drive this technique through.

# Given string
given_string = "FastAPI"

# Extract the first two characters
first_two_chars = given_string[:2]

# Extract the last two characters
last_two_chars = given_string[-2:]

# Create the new string
new_string = first_two_chars + last_two_chars

# Display the new string
print("Original String:", given_string)
print("New String:", new_string)

Output

Original String: FastAPI
New String: FAPI

Explanation

  • Begin with the string "FastAPI."

  • Slicing is used to retrieve the first two letters "Fa."

  • Extract the final two characters "PI" using negative slicing.

  • Concatenate the extracted first and final two characters to form the new string "FAPI."

Applications

1. Data Processing. When handling enormous datasets, identifying certain characters aids in data analysis and processing.

2. User Interface. Manipulating strings in GUI apps. This only shows essential information concisely.

3. Text Formatting. Extracting essential characters from text data improves readability and presentation.

Conclusion

This guide introduced Python string slicing, a useful feature that effectively generates a new string from the beginning and final two characters of an existing string. The syntax is simple, yet it includes complex features like negative indices and step sizes. Understanding this notion will improve your ability to manage strings in Python applications, enhancing performance and substring handling.

Updated on: 09-Aug-2023

811 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements