I am Hack Sparrow
Captain of the Internets.

Python: split string method and examples

string split method and examples in Python

So you are looking to break up a string into smaller strings (in an array if you are coming from a PHP / JavaScript background) based on a delimiter or character. Python provides a very straightforward and easy function to do that. The function is called split, it returns a list as the result (you really don't want to a Python array for this one) and here are some examples. Examples 1 to 3 are to be tried at the Python console.

Example 1: print a list of web technologies

>>> s = 'python,jquery,javascript'
>>> s.split(",")
['python', 'jquery', 'javascript']
>>> a, b, c = s.split(",")
>>> a
>>> b
>>> c

Example 2: extract the domain name components

>>> s = 'irc.freenode.net'
>>> subdomain, domain, tld = s.split('.')
>>> subdomain
>>> domain
>>> tld

Example 3: extract the domain name

>>> s = 'irc.freenode.net'
>>> i = s.split('.', 1)
>>> domain_name = i[1]
>>> domain_name

Example 4: sing a song

lyrics = 'one! two! three! four!'
nums = lyrics.split(' ', 2)
for num in nums:
print 'I say ' + num

So that's it. Splitting a string in Python is really easy, all you have to do is call the split method on a string object and pass the delimiter and optional maxsplit count.


  1. Arrays in Python are an altogether different beast compared to PHP or JavaScript.
  2. Lists are are Pythons 'arrays', if you want an equivalent to the data type found in PHP or JavaScript.

  1. How do you split 1234567890 into individual numbwer? 😉
  2. How do you split abcdef into individual characters? 😉 😉
  3. How do you split ag373jjsl? 😛
  4. Is your method the best? Really? Can you make it better?
  5. How do you create a string from a list?
  6. How do you create a string from a tuple?
  7. What is the difference between array and list?
  8. What is the difference between array and tuple?

  1. Python list
  2. Python tuple
  3. Python string methods
  4. Python array

8 Responses to “Python: split string method and examples”

  1. Alvaro says:

    data = serialport.readline().decode(“utf-8”)
    In,P,Y,R,d = data.split(“,”)

    I have this string comming from serial port and it is first splitted in 5 strings
    then i try to pass the data into 5 different values , but breaks in
    valueError: need more than 1 value to unpack, in last line.

    Can you help me
    Thank you.

  2. Isaure says:


    Do you have the answer for the 1st exercise, please?

    I tried:
    > l=’1234567890′
    > l.split(“”)

    and got:
    ValueError: empty separator


  3. answer says:

    Do you have the answer for the 1st exercise, please?

  4. maryam says:

    how do you great file in python?
    please answer me

  5. Daniel Montoya says:

    The answer to the fist exercise could be:

    joinexample = ‘, ‘.join(‘1234567890’)

    Then you can separate the result with split():

    splitjoin = joinexample.split(‘, ‘)

  6. pradeep; says:


    you should give any sperator in “;” in split(“;”) cotes.

  7. kapil bakshi says:

    hi dear all,
    i have some little question about in python please reply.

    # create a list of all words that occur in the text and assure that each word occurs only once.
    # create a dictionary word : count, find at least 2 methods

    how i will make this programme,


  8. Rizwan says:

    i have a string lyk this
    how i can split it into

Make a Comment