Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 4th, 2006, 9:50 AM   #1
Steveire
Newbie
 
Join Date: Jan 2006
Posts: 13
Rep Power: 0 Steveire is on a distinguished road
string split_once() function?

Hi,

Is there some easy way to split a string by the first occurance of a certain character to make two strings?

If I use
sentence = "What is your quest?"
partOne, partTwo = sentence.split_once(' ')
I'd expect it to give me:
partOne = "What"
partTwo = "is your quest?"

I've been using things like
parts = []
parts = sentence.split(' ')
partOne = parts[0]
to do this. I figure there must be a better way. Any idea?
Steveire is offline   Reply With Quote
Old Mar 4th, 2006, 10:43 AM   #2
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
The split function has options like s.split([ separator[, maxsplit]]):
sentence = "What is your quest?"
partOne, partTwo = sentence.split(' ', 1)
print partOne, partTwo
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Mar 6th, 2006, 11:46 AM   #3
Steveire
Newbie
 
Join Date: Jan 2006
Posts: 13
Rep Power: 0 Steveire is on a distinguished road
Perfect. Thanks.
Steveire is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC