Skip to content
Related Articles

Related Articles

Python – Add space between Numbers and Alphabets in String

View Discussion
Improve Article
Save Article
  • Last Updated : 30 Aug, 2022
View Discussion
Improve Article
Save Article

Given a string consisting of numbers and Strings, add space between them.

Input : test_str = ‘ge3eks4geeks is1for10geeks’ 

Output : ge 3 eks 4 geeks is 1 for 10 geeks 

Explanation : Numbers separated from Characters. 

Input : test_str = ‘ge3eks4geeks’ 

Output : ge 3 eks 4 geeks 

Explanation : Numbers separated from Characters by space.

Method #1: Using regex + sub() + lambda

In this, we perform the task of finding alphabets by appropriate regex and then sub() is used to do replacements, lambda does the task of adding spaces in between.

Python3




# Python3 code to demonstrate working of
# Add space between Numbers and Alphabets in String
# using regex + sub() + lambda
import re
 
# initializing string
test_str = 'geeks4geeks is1for10geeks'
 
# printing original String
print("The original string is : " + str(test_str))
 
# using sub() to solve the problem, lambda used tp add spaces
res = re.sub("[A-Za-z]+", lambda ele: " " + ele[0] + " ", test_str)
 
# printing result
print("The space added string : " + str(res))

Output

The original string is : geeks4geeks is1for10geeks
The space added string :  geeks 4 geeks   is 1 for 10 geeks 

Method #2: Using regex + sub()

This is one of ways to solve. In this, we look for numerics rather than alphabets to perform task of segregation, similar approach by use numerics are search criteria to add spaces.

Python3




# Python3 code to demonstrate working of
# Add space between Numbers and Alphabets in String
# using regex + sub()
import re
 
# initializing string
test_str = 'geeks4geeks is1for10geeks'
 
# printing original String
print("The original string is : " + str(test_str))
 
# using sub() to solve the problem, lambda used tp add spaces
res = re.sub('(\d+(\.\d+)?)', r' \1 ', test_str)
 
# printing result
print("The space added string : " + str(res))

Output

The original string is : geeks4geeks is1for10geeks
The space added string : geeks 4 geeks is 1 for 10 geeks

Method #3: Using replace() method.

When u find a numeric character within string replace that character with space appended on left and right.

Python3




# Python3 code to demonstrate working of
# Add space between Numbers and Alphabets in String
 
# initializing string
test_str = 'ge3eks4geeks'
 
# printing original String
print("The original string is : " + str(test_str))
num="0123456789"
for i in test_str:
    if i in num:
        test_str=test_str.replace(i," "+i+" ")
res=test_str
# printing result
print("The space added string : " + str(res))

Output

The original string is : ge3eks4geeks
The space added string : ge 3 eks 4 geeks

Time Complexity: O(n)

Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!