String function to strip the last comma
PythonPython Problem Overview
Input
str = 'test1,test2,test3,'
Ouput
str = 'test1,test2,test3'
Requirement to strip the last occurence of ','
Python Solutions
Solution 1 - Python
Just use rstrip().
result = your_string.rstrip(',')
Solution 2 - Python
str = 'test1,test2,test3,'
str[:-1] # 'test1,test2,test3'
Solution 3 - Python
The question is very old but tries to give the better answer
str = 'test1,test2,test3,'
It will check the last character, if the last character is a comma it will remove otherwise will return the original string.
result = str[:-1] if str[-1]==',' else str
Solution 4 - Python
Though it is little bit over work for something like that. I think this statement will help you.
str = 'test1,test2,test3,'
result = ','.join([s for s in str.split(',') if s]) # 'test1,test2,test3'