0

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

Edited by persianprez: n/a

3
Contributors
3
Replies
8
Views
7 Years
Discussion Span
Last Post by persianprez
0

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

Edited by Krstevski: n/a

0

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]

Edited by vegaseat: missing '*'

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.