133

I am trying to split this string in python: 2.7.0_bf4fda703454

I want to split that string on the underscore _ so that I can use the value on the left side.

2
  • Read up on partition method of strings, and then update your question. Commented Apr 21, 2011 at 20:08
  • This is not a good canonical, because it is not clear what should happen if there is more than one underscore in the input. Depending on the exact specification, either of the duplicates I have linked is better. Commented Jan 22, 2023 at 13:12

3 Answers 3

163

"2.7.0_bf4fda703454".split("_") gives a list of strings:

In [1]: "2.7.0_bf4fda703454".split("_") Out[1]: ['2.7.0', 'bf4fda703454'] 

This splits the string at every underscore. If you want it to stop after the first split, use "2.7.0_bf4fda703454".split("_", 1).

If you know for a fact that the string contains an underscore, you can even unpack the LHS and RHS into separate variables:

In [8]: lhs, rhs = "2.7.0_bf4fda703454".split("_", 1) In [9]: lhs Out[9]: '2.7.0' In [10]: rhs Out[10]: 'bf4fda703454' 

An alternative is to use partition(). The usage is similar to the last example, except that it returns three components instead of two. The principal advantage is that this method doesn't fail if the string doesn't contain the separator.

Sign up to request clarification or add additional context in comments.

Comments

103

Python string parsing walkthrough

Split a string on space, get a list, show its type, print it out:

el@apollo:~/foo$ python >>> mystring = "What does the fox say?" >>> mylist = mystring.split(" ") >>> print type(mylist) <type 'list'> >>> print mylist ['What', 'does', 'the', 'fox', 'say?'] 

If you have two delimiters next to each other, empty string is assumed:

el@apollo:~/foo$ python >>> mystring = "its so fluffy im gonna DIE!!!" >>> print mystring.split(" ") ['its', '', 'so', '', '', 'fluffy', '', '', 'im', 'gonna', '', '', '', 'DIE!!!'] 

Split a string on underscore and grab the 5th item in the list:

el@apollo:~/foo$ python >>> mystring = "Time_to_fire_up_Kowalski's_Nuclear_reactor." >>> mystring.split("_")[4] "Kowalski's" 

Collapse multiple spaces into one

el@apollo:~/foo$ python >>> mystring = 'collapse these spaces' >>> mycollapsedstring = ' '.join(mystring.split()) >>> print mycollapsedstring.split(' ') ['collapse', 'these', 'spaces'] 

When you pass no parameter to Python's split method, the documentation states: "runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace".

Hold onto your hats boys, parse on a regular expression:

el@apollo:~/foo$ python >>> mystring = 'zzzzzzabczzzzzzdefzzzzzzzzzghizzzzzzzzzzzz' >>> import re >>> mylist = re.split("[a-m]+", mystring) >>> print mylist ['zzzzzz', 'zzzzzz', 'zzzzzzzzz', 'zzzzzzzzzzzz'] 

The regular expression "[a-m]+" means the lowercase letters a through m that occur one or more times are matched as a delimiter. re is a library to be imported.

Or if you want to chomp the items one at a time:

el@apollo:~/foo$ python >>> mystring = "theres coffee in that nebula" >>> mytuple = mystring.partition(" ") >>> print type(mytuple) <type 'tuple'> >>> print mytuple ('theres', ' ', 'coffee in that nebula') >>> print mytuple[0] theres >>> print mytuple[2] coffee in that nebula 

Comments

22

If it's always going to be an even LHS/RHS split, you can also use the partition method that's built into strings. It returns a 3-tuple as (LHS, separator, RHS) if the separator is found, and (original_string, '', '') if the separator wasn't present:

>>> "2.7.0_bf4fda703454".partition('_') ('2.7.0', '_', 'bf4fda703454') >>> "shazam".partition("_") ('shazam', '', '') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.