How to split a string using regex in python

By: On:

In this python tutorial, We will discuss how to split a string using regex in python. In my last blog, we discussed How to create a string and assign it to a variable in python

We will also cover the below topics here

  • How to split a string by comma in python
  • How to split a string by space in python
  • How to split a string by delimiter in python
  • How to split a string by character in python
  • How to split a string using split function in python
  • How to split a string last element in python
  • How to split a string every character in python
  • How to split a string multiple separators in python
  • How to split a string by index in python

If you are new to Python, then you can download and install Python, as well as you can create your first hello world program in Python.


Also you can know why python is popular and how does python works.

How to split a string using regex in python

In python, we can split a string using regular expression. Let us see how to split a string using regex in python.

We can use re.split() for the same. re is the module and split() is the inbuilt method in that module.


For example

import re
myvar = 'sky1cloud3blue333red'

print(re.split('\d+', myvar))

Note: Make sure to import the re module or else it will not work.

The output will be

['sky', 'cloud', 'blue', 'red']

See the output here

How to split a string using regex in python
split a string using regex in python

How to split a string by comma in python

We can split the string using comma as a separator in python.

Let’s see an example and see how it,s working

Example:

mystring = "Hey, I love Python, Python is easy"

x = mystring.split(",")

print(x)

The output will be

['Hey', ' I love Python', ' Python is easy']

See the output here

How to split a string using comma in python

How to split a string by space in python

You can also split a string by using space as a separator in python.

By default the split() method uses space as a separator while splitting the string.

For example:

mystring = "sky blue red cloud"

x = mystring.split()

print(x)

The output will be

['sky', 'blue', 'red', 'cloud']

Example:

Split the string into a list

mystring = "sky blue red cloud"

x = mystring.split()

for temp in mystring:
    print(temp)

The output will be

s
k
y
 
b
l
u
e
 
r
e
d
 
c
l
o
u
d

See here the output

how to split the string using space as a separator

How to split a string by delimiter in python

We can split the string by delimiter

Example:

myvar = 'I love python'

# This splits at space
print(myvar.split())

nature = 'sky, cloud, blue'

# This splits at ','
print(nature.split(','))

nature = 'sky:cloud:blue'

# This splits at ':'
print(nature.split(':'))

The output will be

['I', 'love', 'python']
['sky', ' cloud', ' blue']
['sky', 'cloud', 'blue']

See below for the output

How to split string using delimiter

How to split a string by every character in python

Let’s discuss here, how to split the string with array of character or list of character.

1- How to split a string into array of characters using for loop

We can split a string into array of characters using for loop.

Example:

def charactersplit(str):
  return [ch for ch in str]

mystr = 'I love python'

print(charactersplit(mystr))

Output will be

['I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'y', 't', 'h', 'o', 'n']

See output here

How to split string into array of characters using for loop

2- How to split string into array of characters by typecasting string to list

We can split a string into array of characters by typecasting string to list.

Example:

def charactersplit(str):
  return list(str)

mystr = 'I love python'

print(charactersplit(mystr))

Output will be

['I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'y', 't', 'h', 'o', 'n']

See output here

How to split string into array of characters by typecasting string to list

How to split a string by split function in python

We can split a string by split function in python.

For example:

mystring = "sky cloud  blue red"

x = mystring.split()

print(x)

Output will be

['sky', 'cloud', 'blue', 'red']

See output here

How to split a string by split function in python

How to split a string by the last element in python

We can split the last element of a string in python. Let’s discuss on the same

1- How to split a string by the last element using rsplit()

We can use rsplit() function to split the last element of a string in python.

Example:

# initializing string
mystring = "sky, cloud,  blue, red"

# rsplit() to split the string
result = mystring.rsplit(', ', 1)

print(str(result))

The output will be

['sky, cloud,  blue', 'red']

See the output here

How to split a string by the last element using rsplit()

2- How to split a string by the last element using rpartition()

We can use rpartition() function to split the last element of a string in python.

Example:

# initializing string
mystring = "sky, cloud,  blue, red"

# rsplit() to split the string
result = mystring.rpartition(', ')

print(str(result))

Note: rpartition() will take only one argument or else it will show you an exception “TypeError: rpartition() takes exactly one argument (2 given)”

The output will be

('sky, cloud,  blue', ', ', 'red')

See the output here

How to split a string by the last element using rpartition()

How to split a string by multiple separators in python

We can use split() to split the string with one separator at a time.

If you want to use multiple separator to split the string we can use re.split() instead of split().

For Example

import re
myvar = 'sky,cloud;blue,   red'

print(re.split(r'[;,\s]\s*', myvar))

The output will be

['sky', 'cloud', 'blue', 'red']

See the output here

How to split a string by multiple separators in python

How to split a string by index in python

We can also split a string by index in python.

Example 1:

mystring = 'I love python It is easy to learn'
indices = [0,6,13]
x = [mystring[index:] for index in indices]
for mystring in x:
    print (mystring)

The output will be

I love python It is easy to learn
 python It is easy to learn
 It is easy to learn
How to split a string by indices in python

See the above output

Example 2:

mystring = 'I love python It is easy to learn'
indices = [0,6,13]
x = [mystring[i:j] for i,j in zip(indices, indices[1:]+[None])]
for mystring in x:
    print (mystring)

Output will be

I love
 python
 It is easy to learn

See the output here

How to split a string by index in python

You may like following below Python Tutorials:

Conclusion

Python is the most popular open-source object-oriented programming language and it is easy to learn and syntax wise it is very simple.

We can use re.split() for the same. re is the module and split() is the inbuilt method in that module.

We can split the string using comma,space and other delimiter as a separator in python.


We can use rsplit() and rpartition() functions to split the last element of a string in python

This python tutorial explains the below points:

  • How to split a string using regex in python
  • How to split a string by comma in python
  • How to split a string by space in python
  • How to split a string by delimiter in python
  • How to split a string by every character in python
  • How to split a string using split function in python
  • How to split a string last element in python
  • How to split a string multiple separators in python
  • How to split a string by index in python

Leave a Comment