Main Tutorials

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Cliff howell
5 years ago

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!

Lakshmi Harita
4 years ago

How can i split a string like……”mynameisjohn” into words and store in a list?

Harith Shah
4 years ago
Reply to  Lakshmi Harita

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

Tamasha Fernando
4 years ago

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

harish
2 years ago

first split by , and then by – and alter the month date year in the sub list

LEO
4 years ago

really simple but useful ….THX

2 years ago

str1 = ‘abcd.ef[2].gh[0][1].ijkl’ 
write a program to split this into an array [‘abcd’, ‘ef’, 2, ‘gh’, 0, 1,’ijkl’]

anonymous
3 years ago

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)

manjula
5 years ago

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

koko
4 years ago
Reply to  manjula

Short answer:
* extract small words: re.findall(r”(?:\s|:)([A-Za-z\d]{1,4})(?:\s)”, text)
* extract mac and IP – re.compile(r'(?:[0-9a-fA-F]:?){12}’)

more info:
http://blog.softhints.com/python-advanced-regex-extraction/

koko
4 years ago
Reply to  manjula

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/

yoginandha
6 years ago

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.

Cliff howell
5 years ago
Reply to  yoginandha

ok

Koko
6 years ago
Reply to  yoginandha

I would go for:

number = “1000,000,000.000.000”
print (re.findall(r”[d’]+”, number))

source: http://blog.softhints.com/python-string-split-by-separator/

abhi
4 years ago

thanks, mkyong

tinus_tech
4 years ago

good stuff