How to split a string without spaces in Python

How to split a string without spaces in Python. If this is the topic you want, this article is for you. I will show you a couple of ways to do that, like using the re.findall() function, the list() function, or the map() function. This article will give you an idea of this topic.

Split a string without spaces in Python

Use the re.findall() function.

Syntax:

re.findall(regex, string))

Parameters:

  • regex: regular expression to search for digits.
  • string: string you want the regular expression to search for.

The findall() function returns a list containing the pattern matches in the string. If not found, the function returns an empty list.

Example:

  • To split a string without spaces into a list of characters, I use the re.findall() function with the RegEx of r’\S’.
import re 

characterStr  = 'learnshareit'

# Use the re.findall() function to split a string without spaces into a list of characters.
result = re.findall(r'\S', characterStr)
print(result)

Output:

['l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't']

Also, with the re.findall() function to split the string into a list of integers, I would change RegEx = r’\d’.

Example:

import re 

numberStr = '12345'

# Use the re.findall() function to split a string without spaces into a list of integers.
result = re.findall(r'\d', numberStr)
print(result)

Output:

['1', '2', '3', '4', '5']

Or, for strings containing uppercase characters without spaces, the re.findall() function also works fine.

Example:

import re

uppercaseStr = 'VisitLearnShareItWebsite'

# Use the re.findall function splits into strings containing uppercase characters.
result = re.findall('[A-Z][^A-Z]*', uppercaseStr)
print('String containing uppercase characters:', result)

Output:

Strings containing uppercase characters: ['Visit', 'Learn', 'Share', 'It', 'Website']

Use the list() function. 

Using the list() function to split a string without spaces into lists of characters or lists of integers.

Example:

characterStr = 'learnshareit'
numberStr = '12345'

# Use the list() function to split strings without spaces to list characters and list of integers.
characterStrList = list(characterStr)
numberList = list(numberStr)
print(characterStrList)
print(numberList)

Output:

['l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't']
['1', '2', '3', '4', '5']

Use the map() function.

Syntax:

map(function, iterable, ...)

Parameters:

  • function: The function to execute for each element in the iterable.
  • iterable: the iterable objects you want to traverse.

Example:

  • I will split the string without spaces using the map() function.
  • The first parameter, map() is an int() function for the numeric string.
  • The first parameter, map() is the str() function for string literals.
  • Then use the list() function to convert the map object to a list.
characterStr = 'learnshareit'
numberStr = '12345'

# Split a string of characters without spaces into a list of characters using the map() function.
characterList = list(map(str, characterStr))
print(characterList)

# Split a string of numbers without spaces into a list of integers using the map() function.
characterList = list(map(str, numberStr))
print(characterList)

Output:

['l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't']
['1', '2', '3', '4', '5']

Summary:

The thread split a string without spaces in Python may end here. You can get the re.findall() function to do this. Please comment below if you have any other questions or ideas for this article.

Leave a Reply

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