How Python Remove Last Character from String in Simple Way

In this article, we will now concentrate on how Python remove last character from string in a simple way.

Introduction

Python’s best feature is string manipulation. String manipulation functions include slicing, looping, and string methods.

In Python, we sometimes need to remove the last character from the string because it was added by accident.

Remove Last Character from String in Python

To remove the last character from a string in Python, use string slicing. The string slicing allows you to obtain a substring, while indexing is used to get individual characters from the string.

Practical Example – Remove Last Word

Imagine a text file with several lines. The file’s final word must also be removed.

Example file:

This is a sample line for testing. LastWord.
This is a sample line for testing. KillingIt.

Example Program:

updated_data = ''

with open('random_text.txt', 'r+') as file:
    file_content = file.readlines()

    for line in file_content:

        updated_line = ' '.join(line.split(' ')[:-1])

        updated_data += f'{updated_line}\n'

    file.seek(0)
    file.truncate()

    file.write(updated_data)

Output:

This is a sample line for testing.
This is a sample line for testing.

3 Easy Ways to Remove the Last Characters of a String in Python

1. Using string slices

In slice, the first character of a string is 0, and the last character is -1 (equal to len(string) – 1).

We may remove the final character from a string by accessing the positive index of the provided string.

x = 'ATGCATTTC'

x[:-1]
'ATGCATTT'

We may also remove or delete the last character by using the negative index.

x[:len(x)-1]   
'ATGCATTT'

2. Using List

Strings can be converted to a list to remove the last character in a string

''.join(list(x)[:-1])
'ATGCATTT'

3. Using the rstrip function to Remove Last Character From String in Python

The string function rstrip is used to remove characters from the right side of the provided string. Therefore, we will use it to remove or delete the string’s last character.

String slices

There are two techniques of string slicing, the first of which uses the built-in slice() function and the second of which uses the [:] array slice method. Python string slicing involves extracting a substring from a given string by slicing it sequentially from beginning to finish.

Example:

Str = "PYTHON FOR FREES"
Remove_last = Str[:-1]
print("String : ",Remove_last)

String : PYTHON FOR FREE

Here, we have taken a sample string str = “PYTHON FOR FREE .” Then for removing the last character of the string, the indexing always starts from -1.

By slicing it from the -1 index, we removed the last character of the string. At last, we print the output. Hence, we see that the unwanted character has been removed.

String rstrip() function

The string rstrip function is used to remove the characters from the right side of the string. So we will be using it to remove or delete the last character of the string. It can be done only in a single line of code and straightforward method to remove the last character from the string.

Example:

str = input("Enter the string : ")
 
remaining_string = str.rstrip(str[-1])
 
print("String : ",remaining_string)

List

In Python, the slice object may be replaced by indexing syntax. This is both a syntax– and execution-friendly method for string slicing using list slicing and Array slicing. The start, end, and step constructors have the same mechanism as slice().

String = 'PYTHONFORFREE'
 
print(String[:6])

Output:

PYTHON

Summary

In this tutorial about Python Remove Last Character from String, we have learned about all the methods through which we can remove a string’s last character. All the methods are explained in deep with the help of examples.

The basic and easiest method was to use positive and negative indexes. You can use any of the methods you like to use according to your program or project requirements.

Leave a Comment