Remove last comma from string python

Actually we have to consider the worst case also.

The worst case is,

str= 'test1,test2,test3, ,,,, '

for above code, please use following code,

result = ','.join([s.strip() for s in str.split(',') if s.strip()!=''])

It will work/remove the prefix 'comma' also. For example,

str= ' , ,test1,test2,test3, ,,,, '

Remove the last comma from a String in Python #

Use the str.rstrip() method to remove the last comma from a string, e.g. new_str = my_str.rstrip(','). The str.rstrip() method will return a copy of the string with the trailing comma removed.

Copied!

my_str = 'bobby,hadz,com,' # ✅ Remove last character from string if it's a comma new_str = my_str.rstrip(',') print(new_str) # 👉️ bobby,hadz,com # ------------------------------------------------ my_str = 'bobby,hadz,com' # ✅ Remove the last comma from a string new_str = ''.join(my_str.rsplit(',', 1)) print(new_str) # 👉️ bobby,hadzcom

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.

Copied!

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() #

To remove the last comma from a string:

  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.

Copied!

my_str = 'bobby,hadz,com' new_str = ''.join(my_str.rsplit(',', 1)) print(new_str) # 👉️ bobby,hadzcom

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

Copied!

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
separator Split the string into substrings on each occurrence of the separator
maxsplit At 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 last comma from string python

💡 Outline

You can remove comma from string in python by using string’sreplace() method.
Syntax:

final_string=initial_string.replace(',',"")

In this tutorial, we will take a sample string with a couple of commas in it and we will see some methods to remove comma from string in Python.

Table of Contents

  • Using replace() function to remove comma from string in Python
  • Using re or RegEx package function to remove comma from string in Python
    • Was this post helpful?

Using replace() function to remove comma from string in Python

There are a lot of built-in functions Python which helps us to carry various operations on strings. One of the built-in functions is the replace() function. This function is used to replace one string with another and return the new string. The changes that are to be made are mentioned in the function argument.

Example:

initial_string="re,move com,mas f,rom this, string"

final_string=initial_string.replace(',',"")

print(final_string)

Output:

remove commas from this string

Explanation

  • Initialized sample string to initial_string
  • Used replace() function to replace commas in string and assigned it to another variable final_string
  • As you can see, we have replaced the comma with empty string.
  • Printed the final_string

Using re or RegEx package function to remove comma from string in Python

The re or RegEx package is a built-in python package that helps in dealing with regular expressions. This package helps in the manipulation of special sequences of characters and strings.

This package has a function called the re.sub() function which helps in replacing characters that keep on repeating again and again with another character or sub-string.
Example:

import re

initial_string="re,move com,mas f,rom this, string"

print(re.sub(",","",initial_string))

Output:

remove commas from this string

Explanation

  • Imported RegEx package that is pre-installed in python
  • Stored string in variable named initial_string
  • Used re.sub method to remove commas in string. Here, we have replaced all the commas with empty string.
  • You can either assign returned value of the re.sub method or directly print it as we did in above code.

That’s all about how to remove comma from String in Python.

How do I remove the last comma in Python?

You can remove comma from string in python by using string's replace() method.

How do I remove the last comma from a string?

To remove the last comma from a string, call the replace() method with the following regular expression /,*$/ as the first parameter and an empty string as the second. The replace method will return a new string with the last comma removed. Copied!

How do I remove the last occurrence of a character from a string in Python?

rstrip. The string method rstrip removes the characters from the right side of the string that is given to it. So, we can use it to remove the last element of the string. We don't have to write more than a line of code to remove the last char from the string.