Remove First and Last Character of String in Python

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.

Output:

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.

Output:

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.

Output:

Instead of using a for loop and the append() method to create tempList from inputList, you can use list slicing as shown below.

Output:

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. 

Output:

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.

Output:

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!

Was this post helpful?

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.