Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String function to strip the last comma

Tags:

python

Input

str = 'test1,test2,test3,' 

Ouput

str = 'test1,test2,test3' 

Requirement to strip the last occurence of ','

like image 621
user1050619 Avatar asked Sep 27 '12 16:09

user1050619


People also ask

How do I remove the last character of a string?

The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

How do I remove the last comma in a string in C++?

To easily remove the last comma you can use the '\b' character.

How do I remove the last comma from a string in python?

To remove commas from a string in Python, we can use the replace() method or the re package.


1 Answers

Just use rstrip().

result = your_string.rstrip(',') 
like image 139
Amber Avatar answered Sep 29 '22 23:09

Amber