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 146 views
2 answers 122 views
1 answer 103 views
3 answers 136 views
2 answers 106 views
1 answer 110 views
...