How to remove the leading and trailing commas from a string in Python

3 Answers

0 votes
s = ",,,Python,,"

s = s.strip(',')

print(s)  


  
'''
run:
  
Python
  
'''

 



answered Mar 6, 2025 by avibootz
0 votes
s = ",,,Python,,"

s = s.lstrip(',').rstrip(',')

print(s)  


  
'''
run:
  
Python
  
'''

 



answered Mar 6, 2025 by avibootz
0 votes
import re

s = ",,,Python,,"

s = re.sub(r'^,+|,+$', '', s)

print(s)  



  
'''
run:
  
Python
  
'''

 



answered Mar 6, 2025 by avibootz

Related questions

3 answers 275 views
2 answers 230 views
1 answer 179 views
3 answers 273 views
2 answers 204 views
1 answer 189 views
...