Specific Characters Printing With Python

given a string as shown below,


how to print it like shown below ?


The original string will always maintain the above-mentioned format.



I did not realize you had periods and commas... that adds a bit of trickery. You have to split on the periods too

I would use something like this...

list_to_parse = "[xyx],[abc].[cfd],[abc].[dgr],[abc]"

count = 0
for  i in list_to_parse.split('.'):
    for j in i.split(','):
        string = str(count + 1) + "." + j
        if string:
            count += 1
        string = None

Another option is split on the left bracket, and then just re-add it with enumerate - then strip commas and periods - this method is also probably a tiny bit faster, as it's not a loop inside a loop

list_to_parse = "[xyx],[abc].[cfd],[abc].[dgr],[abc]"

for index, i in enumerate(list.split('[')):
    if i:
        print(str(index) + ".[" + i.rstrip(',.'))

also strip is really "what characters to remove" not a specific pattern. so you can add any characters you want removed from the right, and it will work through the list until it hits a character it can't remove. there is also lstrip() and strip()

string manipulation can always get tricky, so pay attention. as this will output a blank first object, so index zero isn't printed etc... always practice and learn your needs :D