-3

I have a python file with data reading that I want to read into Python. I have lists that contain strings like "apple , 2.99 ". But the goal is to turn that into "apple - $2.99" in the output. Do I have to use some type of dictionary? Please help.

How do I read this into Python?

lime
  • 801
  • 8
  • 21
Quez
  • 1
  • 1
  • 3
    `your_string.replace(',', '-')`? – Green Cloak Guy Sep 16 '20 at 18:51
  • 2
    What you are describing does not seem to match your title. – khelwood Sep 16 '20 at 18:51
  • Possible duplicates: https://stackoverflow.com/questions/10017147/removing-a-list-of-characters-in-string, https://stackoverflow.com/questions/1228299/changing-one-character-in-a-string, https://stackoverflow.com/questions/12723751/replacing-instances-of-a-character-in-a-string – Aragorn Crozier Sep 16 '20 at 18:58

2 Answers2

0

Regarding what asked in the post, use replace to replace all commas to the desired " - $":

products = ["apple , 2.99 ", "banana , 3.99 "]

# This is lis comprehension, looping on the list products, and replacing
products = [p.replace(", ", "- $") for p in products]

# ['apple - $2.99 ', 'banana - $3.99 ']
print(products)
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
0

Yeah, definetely the best way is using string.replace('.', ',').

NGeorg
  • 21
  • 2