how to split string with comma in python 1

how to split string with comma in python

# We can use the split() function and put a "," in the parameter 
# It'll return a list with the string split up by commas.
txt = "hello, my name is Peter, I am 26 years old"

x = txt.split(",")
print(x)

Here is what the above code is Doing:
1. We create a variable called txt, and assign a string to it.
2. We create another variable called x, and assign the value of txt.split(“,”) to it.
3. The split() function splits the string at the specified separator, and returns a list of strings.
4. The separator is a comma: “,”.
5. So, the string is split at every occurrence of a comma.
6. The result is a list, and each item of the list is a part of the original string.

Similar Posts