How can I split and parse a string in Python?

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.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.

Python String, split() method returns a list of strings after breaking the given string by the specified separator. Syntax : str.split(separator, maxsplit). Parameters : Definition and Usage. The split () method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

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

How can I split and parse a string in Python?, "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� Python string method split () returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.

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', '', '')

Python String split() Method, The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will� By using the following method you can split and parse a string in Python "2.7.0_bf4fda703454".split("_") gives a list of strings: >>"2.7.0_bf4fda703454".split("_") ['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).

How can I split and parse a string in Python?, By using the following method you can split and parse a string in Python "2.7. 0_bf4fda703454".split("_") gives a list of strings:. Questions: 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.

Python 3 - String split() Method, Python 3 - String split() Method - The split() method returns a list of all the words in the string, using str as the separator (splits on all whitespace if left� Python | String Split including spaces; Python - Split String of list on K character; Python | Split given string into equal halves; Python | Rear stray character String split; Split a string in equal parts (grouper in Python) Python | Split strings and digits from string list; Python | Split CamelCase string to individual strings; Python | Split string in groups of n consecutive characters; Python | Pandas Split strings into two List/Columns using str.split() Python | Ways to split a string

Python String split() Method, Python String split() Method - Python string method split() returns a list of all the words in the string, using str as the separator (splits on all whitespace if left� When splitting string, it takes one string and break it into two or more lists of substrings. Some time we may need to break a large string into smaller strings. For splitting string python provides a function called split (). What Is split () function ?

Comments
  • Read up on partition method of strings, and then update your question.