Table of Contents
We use strings in python to handle text data. For processing the data, we sometimes need to remove one or more characters from the string. In this article, we will discuss how we can remove the first and the Last Character of string in python. We will also look at how we can remove the first and the last word of string in python.
How to Remove First and Last Character of string in python?
To remove first and Last of string in python, we can use two approaches. The first approach consists of using a for loop and the second approach uses slicing to remove first and last character of the string. Let us discuss both these approaches one by one.
Remove First and Last Character of string Using For Loop
To remove the first and the Last Character of string using a for loop, we will first create an empty string say outputStr
. After that, we will find the length of the input string using the len()
function. The len()
function takes the string as the input argument and returns the length of the string.
After getting the length of the string, we will iterate over the input string. We will start from the second character and will proceed till the second last character. While iterating over the characters in the input string, we will concatenate each character to the outputStr using the + operator.
After execution of the for loop, we will get the final string outputStr
that contains all the characters of the input string except the first and the last character. You can observe this in the following example.
1 2 3 4 5 6 7 8 9 10 11 12 |
myString = "-Java2blog-" print("The input string is:") print(myString) outputStr = "" strLen = len(myString) for i in range(1, strLen - 1): outputStr = outputStr + myString[i] print("The output string is:") print(outputStr) |
Output:
1 2 3 4 5 6 |
The input string is: -Java2blog- The output string is: Java2blog |
Remove First and Last Character of string Using Slicing
Instead of using the for loop, you can also use slicing to remove the first and the last character of the input string. In this approach, we will first find the length of the input string using the len()
function. After that, we will slice out the second to the second last character, leaving behind the first and the last character. We will assign the sliced string to the outputStr as shown below.
1 2 3 4 5 6 7 8 9 |
myString = "-Java2blog-" print("The input string is:") print(myString) strLen = len(myString) outputStr = myString[1:strLen-1] print("The output string is:") print(outputStr) |
Output:
1 2 3 4 5 6 |
The input string is: -Java2blog- The output string is: Java2blog |
Further reading:
Remove First and Last word of string in Python
To remove first and last word of string, We just have to remove the characters before the first space character and the characters after the last space character. We can also use various approaches for this task, as discussed below.
To remove the first and the last word of a string, we will first convert the string into a list of words, say inputList
using the split()
method. The split()
method, when invoked on a string, takes a character that works as the separator as its input argument. Upon execution, it splits the string at each instance where the separator is present and returns a list of substrings.
After obtaining the list of substrings, we will find the length of the list using the len()
function. The len()
function takes the list as its input argument and returns the length of the list.
After that, we will create an empty list, say tempList
. Then, we will iterate over the inputList
using a for loop. We will start from the second element of inputList
and iterate will iterate through inputList
till the second last element. While iterating over the list, we will append each element to tempList
using the append()
method. The append()
method, when invoked on a list, takes an element as its input argument and appends it to the list.
After execution of the for loop, tempList
contains all the words of the input string except the first and the last word. To get the output string from tempList
, we will first create a string, say outputStr
, and will initialize it to the first element of tempList
. After that, we will iterate over the rest of the elements in tempList
using a for loop. While iteration, we will add each element of tempList
to the outputStr
, separated by a space character.
After execution of the for loop, we will get the output string in outputStr as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
myString = "Is Java2blog the best website for programming tutorials?" print("The input string is:") print(myString) inputList = myString.split() print("The list of words is:") print(inputList) listLen = len(inputList) tempList = [] for i in range(1, listLen - 1): tempList.append(inputList[i]) print("The temporary list is:") print(tempList) outputStr = tempList[0] tempLen = len(tempList) for i in range(1, tempLen): outputStr = outputStr + " " + tempList[i] print("The output string is:") print(outputStr) |
Output:
1 2 3 4 5 6 7 8 9 10 |
The input string is: Is Java2blog the best website for programming tutorials? The list of words is: ['Is', 'Java2blog', 'the', 'best', 'website', 'for', 'programming', 'tutorials?'] The temporary list is: ['Java2blog', 'the', 'best', 'website', 'for', 'programming'] The output string is: Java2blog the best website for programming |
Instead of using a for loop and the append()
method to create tempList
from inputList
, you can use list slicing as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
myString = "Is Java2blog the best website for programming tutorials?" print("The input string is:") print(myString) inputList = myString.split() print("The list of words is:") print(inputList) listLen = len(inputList) tempList = inputList[1:listLen-1] print("The temporary list is:") print(tempList) outputStr = tempList[0] tempLen = len(tempList) for i in range(1, tempLen): outputStr = outputStr + " " + tempList[i] print("The output string is:") print(outputStr) |
Output:
1 2 3 4 5 6 7 8 9 10 |
The input string is: Is Java2blog the best website for programming tutorials? The list of words is: ['Is', 'Java2blog', 'the', 'best', 'website', 'for', 'programming', 'tutorials?'] The temporary list is: ['Java2blog', 'the', 'best', 'website', 'for', 'programming'] The output string is: Java2blog the best website for programming |
Instead of using the for loop to create outputStr
from tempList
, you can use the join()
method. The join()
method, when invoked on a string, say separator
, takes an iterable object having all its elements of string data type as its input argument. After execution, it returns an output string in which all the elements of the iterator as separated by the separator
.
In our program, we will use the space character “ ”
as the separator to obtain outputStr
from tempList
as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
myString = "Is Java2blog the best website for programming tutorials?" print("The input string is:") print(myString) inputList = myString.split() print("The list of words is:") print(inputList) listLen = len(inputList) tempList = inputList[1:listLen-1] print("The temporary list is:") print(tempList) outputStr = " ".join(tempList) print("The output string is:") print(outputStr) |
Output:
1 2 3 4 5 6 7 8 9 10 |
The input string is: Is Java2blog the best website for programming tutorials? The list of words is: ['Is', 'Java2blog', 'the', 'best', 'website', 'for', 'programming', 'tutorials?'] The temporary list is: ['Java2blog', 'the', 'best', 'website', 'for', 'programming'] The output string is: Java2blog the best website for programming |
You can also skip creating the tempList
by iterating over the elements of inputList
using a for loop to create the outputStr
as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
myString = "Is Java2blog the best website for programming tutorials?" print("The input string is:") print(myString) inputList = myString.split() print("The list of words is:") print(inputList) listLen = len(inputList) outputStr = inputList[1] for i in range(2,listLen-1): outputStr=outputStr+ " "+ inputList[i] print("The output string is:") print(outputStr) |
Output:
1 2 3 4 5 6 7 8 |
The input string is: Is Java2blog the best website for programming tutorials? The list of words is: ['Is', 'Java2blog', 'the', 'best', 'website', 'for', 'programming', 'tutorials?'] The output string is: Java2blog the best website for programming |
Conclusion
In this article, we have discussed how we can remove the first and the Last Character of string. We also discussed various approaches to remove the first and the last word of a string. Among all the approaches, the approach discussed in the end in both situations is most efficient and I would suggest you use those approaches.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!