C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The split() method with a string argument separates strings based on the specified delimiter.
Note 2: With no arguments, split() separates strings using one or more spaces as the delimiter.
Python program that uses split
# Input string.
s = "topeka,kansas city,wichita,olathe"
# Separate on comma.
cities = s.split(",")
# Loop and print each city name.
for city in cities:
print(city)
Output
topeka
kansas city
wichita
olathe
Python program that uses split, no arguments
# Input string.
# ... Irregular number of spaces between words.
s = "One two three"
# Call split with no arguments.
words = s.split()
# Display results.
for word in words:
print(word)
Output
One
two
three
Methods: We combine the open(), readlines(), and strip() methods. The path passed passed to open should be corrected.
Read FilesInfo: This CSV parser splits each line of text at the commas. It loops and displays the original data and the extracted values.
Input file: Codex.txt
manhattan,the bronx
brooklyn,queens
staten island
Python program that parses CSV file
# Open this file.
f = open("C:\Codex.txt", "r")
# Loop over each line in the file.
for line in f.readlines():
# Strip the line to remove whitespace.
line = line.strip()
# Display the line.
print(line)
# Split the line.
parts = line.split(",")
# Display each part of the line, indented.
for part in parts:
print(" ", part)
Output
manhattan,the bronx
manhattan
the bronx
brooklyn,queens
brooklyn
queens
staten island
staten island
So: When we specify 3, we split off only three times from the right. This is the maximum number of splits that occur.
Tip: The first element in the result list contains all the remaining, non-separated string values. This is unprocessed data.
Python program that uses rsplit
# Data.
s = "Buffalo;Rochester;Yonkers;Syracuse;Albany;Schenectady"
# Separate on semicolon.
# ... Split from the right, only split three.
cities = s.rsplit(";", 3)
# Loop and print.
for city in cities:
print(city)
Output
Buffalo;Rochester;Yonkers
Syracuse
Albany
Schenectady
And: We split the three-line string literal into three separate strings with splitlines(). We print them in a for-loop.
Python program that calls splitlines
# Data.
s = """This string
has many
lines."""
# Split on line breaks.
lines = s.splitlines()
# Loop and display each line.
for line in lines:
print("[" + line + "]")
Output
[ This string ]
[ has many ]
[ lines. ]
Tuple: This has three parts. It has the left part, the delimiter character, and the remaining string data.
Also: The rpartition() method is available. It acts from the right of the string, rather than the left. Partition is "lpartition."
Python program that uses partition
# Input data.
s = "123 Oak Street, New York"
# Partition on first space.
t = s.partition(" ")
# Print tuple contents.
print(t)
# Print first element.
print("First element:", t[0])
Output
('123', ' ', 'Oak Street, New York')
First element: 123
Here: In this example, we continue to consume each word in a source string. We read in each word at a time.
While: We use the while-loop to continue as long as further data exists in the input string.
WhilePython program that uses partition, while-loop
# The input string.
s = "The Dev Codes website"
# Continue while the string has data.
while len(s) > 0:
# Partition on first space.
t = s.partition(" ")
# Display the partitioned part.
print(t[0])
print(" ", t)
# Set string variable to non-partitioned part.
s = t[2]
Output
Dot
('Dot', ' ', 'Net Perls website')
Net
('Net', ' ', 'Perls website')
Perls
('Perls', ' ', 'website')
website
('website', '', '')
Here: We sum the integers in a string. The float built-in handles numbers with decimal places.
FloatPython program that splits string with numbers
numbers = "100,200,50"
# Split apart the numbers.
values = numbers.split(",")
# Loop over strings and convert them to integers.
# ... Then sum them.
total = 0
for value in values:
total += int(value)
print(total)
Output
350
Version 1: This version of the code uses split() with no arguments, which splits the string apart on spaces.
Version 2: This code uses the space argument. It has the same logical effect as version 1.
Result: We find that split() with no arguments is faster (by about 10%) than split with a space argument.
Python program that times split
import time
# Input data.
s = "This is a split performance test"
print(s.split())
print(s.split(" "))
# Time 1.
print(time.time())
# Version 1: default version.
i = 0
while i < 1000000:
words = s.split()
i += 1
# Time 2.
print(time.time())
# Version 2: explicit space version.
i = 0
while i < 1000000:
words = s.split(" ")
i += 1
# Time 3.
print(time.time())
Output
['This', 'is', 'a', 'split', 'performance', 'test']
['This', 'is', 'a', 'split', 'performance', 'test']
1361813180.908
1361813181.561 split() = 0.6530 s
1361813182.307 split(" ") = 0.7460 s