Remove the last comma from a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Remove the last comma from a String in Python
  2. Remove the last comma from a String using str.rsplit()
  3. Remove the last comma from a String using slicing

# Remove the last comma from a String in Python

Use the str.rstrip() method to remove the last comma from a string, e.g. my_str.rstrip(',').

The str.rstrip() method will return a copy of the string with the trailing comma removed.

main.py
my_str = 'bobby,hadz,com,' new_str = my_str.rstrip(',') print(new_str) # ๐Ÿ‘‰๏ธ bobby,hadz,com

remove last comma from string

The code for this article is available on GitHub

The first example uses the str.rstrip() method to remove the last character from a string if it's a comma.

The str.rstrip() method takes a string containing characters as an argument and returns a copy of the string with the specified trailing characters removed.

main.py
my_str = 'bobbyhadz.com' result = my_str.rstrip('ocm.') print(result) # ๐Ÿ‘‰๏ธ 'bobbyhadz'

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

Note that the str.rstrip() method only removes the comma if it's the last character in the string.

The str.rstrip() method would remove all trailing commas from the string, not just the last one.

Alternatively, you can use the str.rsplit() method.

# Remove the last comma from a String using str.rsplit()

This is a two-step process:

  1. Use the str.rsplit() method to split the string on the comma, once, from the right.
  2. Use the str.join() method to join the list into a string.
main.py
my_str = 'bobby,hadz,com' new_str = ''.join(my_str.rsplit(',', 1)) print(new_str) # ๐Ÿ‘‰๏ธ bobby,hadzcom

remove last comma from string using str rsplit

The code for this article is available on GitHub

The str.rsplit() method returns a list of the words in the string using the provided separator as the delimiter string.

main.py
my_str = 'bobby hadz com' print(my_str.rsplit(' ')) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] print(my_str.rsplit(' ', 1)) # ๐Ÿ‘‰๏ธ ['bobby hadz', 'com']

The method takes the following 2 arguments:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done, the rightmost ones (optional)

Except for splitting from the right, rsplit() behaves like split().

The last step is to use the str.join() method to join the list into a string.

The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used an empty string separator to join the list into a string without a delimiter.

# Remove the last comma from a String using slicing

You can also use string slicing to remove the last comma from a string.

main.py
my_str = 'bobby,hadz,com,' my_str = my_str[:-1] print(my_str) # ๐Ÿ‘‰๏ธ bobby,hadz,com

remove last comma from string using slicing

The code for this article is available on GitHub

The syntax for string slicing is a_string[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

If the start index is omitted, it is considered to be 0, if the stop index is omitted, the slice goes to the end of the string.

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(a_string) - 1.

Negative indices are used to count backward. A negative stop index of -1 means "remove the last character from the string".

The code sample removes the last character regardless if it's a comma.

Use an if statement if you need to check if it's a comma before removing it.

main.py
my_str = 'bobby,hadz,com,' if my_str[-1] == ',': my_str = my_str[:-1] print(my_str) # ๐Ÿ‘‰๏ธ bobby,hadz,com

The code sample only removes the last character from the string if it's a comma.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev