python find substrings based on a delimiter

后端 未结 4 1867
离开以前
离开以前 2021-01-17 17:54

I am new to Python, so I might be missing something simple.

I am given an example:

 string = \"The , world , is , a , happy , place \" 
4条回答
  •  一整个雨季
    2021-01-17 18:26

    Try using the split function.

    In your example:

    string = "The , world , is , a , happy , place "
    array = string.split(",")
    for word in array:
        print word
    

    Your approach failed because you indexed it to yield the string from beginning until the first ",". This could work if you then index it from that first "," to the next "," and iterate across the string that way. Split would work out much better though.

提交回复
热议问题