d_b
d_b

Reputation: 195

Splitting string

I have a string "This is my python experience"

I want to split it to "This is", "my", "python", "experience".

How can I split it into first two words as 1 element and then one each ? Is there any reference document ?

Upvotes: 0

Views: 57

Answers (2)

Tejas Hegde
Tejas Hegde

Reputation: 56

Try this code:

myString = "This is my python experience"
myList = myString.split(" ")
print(myList)
first = myList[0] + " " + myList[1]
second = myList[2]
third = myList[3]
fourth = myList[4]
finalList = [first, second, third, fourth]
print(finalList)

Upvotes: 0

Hayat
Hayat

Reputation: 1639

This might help.

string = "This is my python experience"
splitString = [" ".join(string.split()[:2])] + string.split()[2:]
#output
#['This is', 'my', 'python', 'experience']

Upvotes: 2

Related Questions