Python – How to split a String

Few examples to show you how to split a String into a List in Python.

1. Split by whitespace

By default, split() takes whitespace as the delimiter.


alphabet = "a b c d e f g"
data = alphabet.split() #split string into a list

for temp in data:
    print temp

Output


a
b
c
d
e
f
g

2. Split + maxsplit

Split by first 2 whitespace only.


alphabet = "a b c d e f g"
data = alphabet.split(" ",2) #maxsplit

for temp in data:
    print temp

Output


a
b
c d e f g

3. Split by #

Yet another example.


url = "mkyong.com#100#2015-10-1"
data = url.split("#")

print len(data) #3
print data[0]  # mkyong.com
print data[1]  # 100
print data[2]  # 2015-10-1

for temp in data:
    print temp

Output


3
mkyong.com
100
2015-10-1
mkyong.com
100
2015-10-1

Reference

  1. Python Built-in Types

17 comments on “Python – How to split a String

  1. so this doesn’t work in phython 2 completely it’s missing ‘(‘ so here’s the fixed code

    url = “mkyong.com#100#2015-10-1”
    data = url.split(“#”)

    print (len(data)) #3
    print (data[0]) # mkyong.com
    print (data[1]) # 100
    print (data[2]) # 2015-10-1

    for temp in data:
    print (temp)

    1. You can’t. How does Python know where words split? Python doesn’t understand the English Language. How would python know to split at “my name is john” and not “myna me isjo hn”? It hasn’t learned the word “My” or “Name” or “is” or “John”.

  2. Please tell me how to do the following quiz using Python 2.7

    The input is “1980-05-25,1999-12-30,2012-08-10”
    output should be 05/25/1980
    12/30/1999
    08/10/2012

  3. How would one split a line like ……. soap 13 and put into dictionary … grocery_dict = {‘soap:’13} etc or have multiple values for same key like…
    …. soap water 13 these would be were in grocery store the items would be located. Soap in isle 13 or Soap and water in isle 13 I am trying to write a simple program to recall items I can’t remember when in store!

  4. NO_OF_DEVICES :2
    NO_OF_ELEMENT :8
    Hmi_IP_address :10.201.231.201

    DEVICE_TYPE : OBU
    DEVICE_NAME : HV
    DEVICE_IP : 10.204.225.148
    DEVICE_LOGIN : v2v
    DEVICE_PASSWD : v2v
    DEVICE_PORT : 80
    DEVOCE_MTP : 1
    DEVICE_WLANMAC :”1c:65:9d:a7:f7:79″

    DEVICE_TYPE : OBU
    DEVICE_NAME : RV
    DEVICE_IP : 10.204.225.140
    DEVICE_LOGIN : v2v
    DEVICE_PASSWD : v2v
    DEVICE_PORT : 80
    DEVOCE_MTP : 1
    DEVICE_WLANMAC :”1c:65:9d:a7:f7:b2″
    ~
    ~
    how to extract the right side data from this file python program

    1. Depending on your needs and data you can extract in several parts(because the data is messy):

      (1) Separate extraction
      (1.1) extract small words: re.findall(r”(?:\s|:)([A-Za-z\d]{1,4})(?:\s)”, text) – result – [‘2’, ‘8’, ‘OBU’, ‘HV’, ‘v2v’, ‘v2v’, ’80’, ‘1’, ‘OBU’, ‘RV’, ‘v2v’, ‘v2v’, ’80’, ‘1’]
      (1.2) extract mac and IP – re.compile(r'(?:[0-9a-fA-F]:?){12}’) – result – [‘1c:65:9d:a7:f7:79’, ‘1c:65:9d:a7:f7:b2’]

      (2) extract all at once – more complicated:
      (2.1) find all ocurrences for \: – c = [(m.start(0), m.end(0)) for m in re.finditer(r”[:\s]+”, text)]
      (2.2) list them:

      for i, l in enumerate(c):
      if i % 2 == 0:
      print(text[l[1]:c[i+1][1]])
      list.append(text[l[1]:c[i+1][1]])

      I recommend you to check:

      https://blog.softhints.com/python-regex-cheat-sheet-with-examples/
      https://blog.softhints.com/notepad-regex-find-group-of-words-and-replace/

  5. can you tell me how to split below line given input “1000,000,000.000.000”
    the required output should be like this
    1000
    000
    000
    000
    000

    in this i am using both comma and dot. my string should separate at this two characters.

Leave a Comment

Your email address will not be published. Required fields are marked *