Python – Add space between Numbers and Alphabets in String
In this article, we delve into a practical Python solution to improve text readability by adding spaces between numbers and alphabets in strings by utilizing Python‘s powerful string manipulation capabilities.
Input: test_str = 'ge3eks4geeks is1for10geeks'
Output: ge 3 eks 4 geeks is 1 for 10 geeks
Explanation: Numbers separated from Characters.
Adding Spaces between Numbers and Alphabets in Python
Below is the lists of methods that we can use to solve the following problem:
Add space between Numbers and Alphabets Using replace() method
When u find a numeric character within a string replace that character with a space appended on the left and right.
Python3
test_str = 'ge3eks4geeks'
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
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)
Add space between Numbers and Alphabets using isdigit() and replace() methods
Here we use isdigit()
the method to identify numeric characters and then we use replace()
method to replace the value with the desired space needed.
Python3
test_str = 'ge3e1ks4geeks'
print ( "The original string is : " + str (test_str))
for i in test_str:
if i.isdigit():
test_str = test_str.replace(i, " " + i + " " )
res = test_str
print ( "The space added string : " + str (res))
|
Output
The original string is : ge3e1ks4geeks
The space added string : ge 3 e 1 ks 4 geeks
Time complexity : O(n), where n is length of test_str
Auxiliary space: O(n), where n is the length of the res string.
Inserting space between numbers and letters using List Comprehension
In this, we use list comprehension to add space between numbers and alphabets
Python3
test_str = 'ge3e1ks4geeks'
print ( "The original string is : " + str (test_str))
res = " ".join([" "+i + " " if i.isdigit() else i for i in test_str])
print ( "The space added string : " + str (res))
|
Output
The original string is : ge3e1ks4geeks
The space added string : ge 3 e 1 ks 4 geeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Inserting space between number and letter using Regex with Lambda
In this, we perform the task of finding alphabets by appropriate regex, and then sub() is used to do replacements, and lambda does the task of adding spaces in between.
Python3
import re
test_str = 'geeks4geeks is1for10geeks'
print ( "The original string is : " + str (test_str))
res = re.sub( "[A-Za-z]+" , lambda ele: " " + ele[ 0 ] + " " , test_str)
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
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(n), where n is the length of the input string.
Inserting space between number and letter using regex with sub()
In this, we look for numerics rather than alphabets to perform the task of segregation, a similar approach by using numerics are search criteria to add spaces.
Python3
import re
test_str = 'geeks4geeks is1for10geeks'
print ( "The original string is : " + str (test_str))
res = re.sub( '(\d+(\.\d+)?)' , r ' \1 ' , test_str)
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
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(n), where n is the length of the input string.
Add Space between numbers and letters using isdigit and isalpha methods
Initialize a test string and an empty result string. Print the original string then loop through the test string character by character using a for loop. For each character in the test string, check if the current character is a digit and the previous character is an alphabet If the conditions in step 4 are true, add a space to the result string then check if the current character is an alphabet and the previous character is a digit if the conditions in step 6 are true, add a space to the result string then add the current character to the result string and print the result string.
Python3
test_str = 'geeks4geeks is1for10geeks'
result = ''
print ( "The original string is : " + str (test_str))
for i in range ( len (test_str)):
if test_str[i].isdigit() and result and result[ - 1 ].isalpha():
result + = ' '
elif test_str[i].isalpha() and result and result[ - 1 ].isdigit():
result + = ' '
result + = test_str[i]
print (result)
|
Output
The original string is : geeks4geeks is1for10geeks
geeks 4 geeks is 1 for 10 geeks
Time complexity: O(n), where n is the length of the input string.
Space complexity: O(n), where n is the length of the input string.
Add Space between numbers and letters using ord() and replace() methods
Check whether the character is a digit using ord() and then replace the digit in a string by adding space front and back to that digit then display the replaced string
Python3
test_str = 'ge3eks4geeks'
print ( "The original string is : " + str (test_str))
digs = [ 48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 ]
for i in test_str:
if ord (i) in digs:
test_str = test_str.replace(i, " " + i + " " )
res = test_str
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)
Inserting space between number and letter using regular expressions and re.sub()
We initialize the input string “test_str” with the given string “ge3eks4geeks” and then use the re.sub() method to search for a pattern of one or more digits in the input string. The pattern is defined using the regular expression ‘(\d+)’, which matches one or more digits. The second argument to the re.sub() method is a replacement string. Here, we use r’ \1 ‘ as the replacement string, which adds a space before and after the matched digits. Finally, the re.sub() method returns the modified string “res”.
Python3
import re
test_str = 'ge3eks4geeks'
print ( "The original string is : " + str (test_str))
res = re.sub( '(\d+)' , r ' \1 ' , test_str)
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), where n is the length of the input string “test_str”. The regular expression matching takes linear time in the length of the string.
Auxiliary space: O(n), as we create a new string “res” to store the modified string.
Inserting space between number and letter using the Recursive method
If the input string test_str is empty, return an empty string. If the first character of test_str is a digit, add a space before and after the digit, and then recursively call the function on the remaining string. If the first character is not a digit, simply append it to the result of the recursive call on the remaining string. Return the result.
Python3
def add_space_recursive(test_str):
if not test_str:
return ""
elif test_str[ 0 ].isdigit():
return " " + test_str[ 0 ] + " " + add_space_recursive(test_str[ 1 :])
else :
return test_str[ 0 ] + add_space_recursive(test_str[ 1 :])
test_str = 'ge3eks4geeks'
print ( "The original string is : " + str (test_str))
result = add_space_recursive(test_str)
print ( "The space added string : " + str (result))
|
Output
The original string is : ge3eks4geeks
The space added string : ge 3 eks 4 geeks
Time complexity: O(n), where n is the length of the input string test_str.
Space complexity: O(n), where n is the length of the input string test_str.
Inserting space between number and letter using group by
Initialize an empty list to store the groups. Use a for loop to iterate through each character in the string. Use the groupby function from itertools module to group the consecutive digits together and add a space before and after the digits. Append each group to the list. then use the join method to combine the list into a single string.
Python3
from itertools import groupby
test_str = 'geeks4geeks is1for10geeks'
print ( "The original string is : " + str (test_str))
res = ' '.join([' ' + i + ' ' if i.isdigit() else i for k, g in groupby(test_str) for i in g])
print ( "The space added string : " + str (res))
|
Output
The original string is : geeks4geeks is1for10geeks
The space added string : geeks 4 geeks is 1 for 1 0 geeks
Time Complexity: O(n) The time complexity of this solution is O(n) because we are iterating through each character in the string once.
Auxiliary Space: O(n) The space complexity of this solution is also O(n) because we are creating a list to store the groups, which can have a maximum length of n.
Last Updated :
24 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...