-1

Here is my list ['k:1','d:2','k:3','z:0'] now I want to remove apostrophes from list item and store it in the string form like 'k:1 , d:2, k:3, z:0' Here is my code

nlist = ['k:1','d:2','k:3','z:0']
newlist = []
for x in nlist:
  kk = x.strip("'")
  newlist.append(kk)

This code still give me the same thing

zircon
  • 742
  • 1
  • 10
  • 22
  • 5
    You sound a bit confused as to how strings are represented. – Chris Sep 16 '20 at 18:55
  • The items in the list are Strings. The apostrophes denote the start and end of each strings, but aren't actually in the strings themselves. You should `print(x)` in your for loop, which will print each element in the list on a line. – the_storyteller Sep 16 '20 at 18:58
  • The `'` is not part of the string and thus it is not something you can remove from it. – darcamo Sep 16 '20 at 18:58
  • I corrected my question please reconsider it Thankyou @darcamo,@the_storyteller,@Chris – zircon Sep 16 '20 at 19:01

4 Answers4

1

Just do this : print(', '.join(['k:1','d:2','k:3','z:0']))

baduker
  • 19,152
  • 9
  • 33
  • 56
0

if you want to see them without the apostrophes, try to print one of them alone.

try this:

print(nlist[0]) output: k:1

you can see that apostrophes because it's inside a list, when you call the value alone the text comes clean.

I would recommend studying more about strings, it's very fundamental to know how they work.

azro
  • 53,056
  • 7
  • 34
  • 70
NGeorg
  • 21
  • 2
0

The parenthesis comes from the way of representing a list, to know wether an element is a string or not, quotes are used

print(['aStr', False, 5]) # ['aStr', False, 5]

To pass from ['k:1','d:2','k:3','z:0'] to k:1 , d:2, k:3, z:0 you need to join the elements.

values = ['k:1','d:2','k:3','z:0']
value = ", ".join(values)
print(value)  # k:1, d:2, k:3, z:0
azro
  • 53,056
  • 7
  • 34
  • 70
0

What you have is a list of strings and you want to join them into a single string.

This can be done with ", ".join(['k:1','d:2','k:3','z:0']).

darcamo
  • 3,294
  • 1
  • 16
  • 27