I'm trying to figure this out, but every google I do just brings up garbage.

What I'm trying to do is split a string at an *

I want the string to be in 2 parts basically. So..

Say the string is 2478*72-432*823-74

What I want to do is split it right after the first * and have the 2 new strings as new strings

So it would be:

given = F4K8*72-432*82T-74

split given @ *

part1 = F4K8*
part2 = 72-432*82T-74

Recommended Answers

All 3 Replies

Try this:

string = 'F4K8*72-432*82T-74'
index = string.find('*')
part1 = string[0:index+1]
part2 = string[index+1:len(string)]
# string[startIndex:endIndex]
print 'Part1: ' + part1
print 'Part2: ' + part2

You could split the string once ...

mystr = "F4K8*72-432*82T-74"

mylist = mystr.split("*", 1)

print mylist   # ['F4K8', '72-432*82T-74']

# add the missing '*' back in
print "part1 = %s*" % mylist[0]
print "part2 = %s" % mylist[1]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.