How can I split a string at the first occurrence of a letter in Python?
Solution 1
Using re.search
:
import re
strs = ["71 1 * abwhf", "8 askg", "*14 snbsb", "00ab"]
def split_on_letter(s):
match = re.compile("[^\W\d]").search(s)
return [s[:match.start()], s[match.start():]]
for s in strs:
print split_on_letter(s)
The regex [^\W\d]
matches all alphabetical characters.
\W
matches all non-alphanumeric characters and \d
matches all numeric characters. ^
at the beginning of the set inverts the selection to match everything that is not (non-alphanumeric or numeric), which corresponds to all letters.
match
searches the string to find the index of the first occurrence of the matching expression. You can slice the original string based on the location of the match to get two lists.
Solution 2
The only way I can think of is to write the function yourself:
import string
def split_letters(old_string):
index = -1
for i, char in enumerate(old_string):
if char in string.letters:
index = i
break
else:
raise ValueError("No letters found") # or return old_string
return [old_string[:index], old_string[index:]]
Solution 3
Use re.split()
import re
strings = [
"71 1 * abwhf",
"8 askg",
"*14 snbsb",
"00ab",
]
for string in strings:
a, b, c = re.split(r"([a-z])", string, 1, flags=re.I)
print(repr(a), repr(b + c))
Produces:
'71 1 * ' 'abwhf'
'8 ' 'askg'
'*14 ' 'snbsb'
'00' 'ab'
The trick here is we're splitting on any letter but only asking for a single split. By putting the pattern in parentheses, we save the split character which would normally be lost. We then add the split character back onto the front of the second string.
Comments
-
LJD200 almost 2 years
A have a series of strings in the following format. Demonstration examples would look like this:
71 1 * abwhf
8 askg
*14 snbsb
00ab
I am attempting to write a Python 3 program that will use a for loop to cycle through each string and split it once at the first occurrence of a letter into a list with two elements.
The output for the strings above would become lists with the following elements:
71 1 *
andabwhf
8
andaskg
*14
andsnbsb
00
andab
There is supposed to be a space after the first string of the first three examples but this only shows in the editor
How can I split the string in this way?
Two posts look of relevance here:
The first answer for the first question allows me to split a string at the first occurrence of a single character but not multiple characters (like all the letters of the alphabet).
The second allows me to split at the first letter, but not just one time. Using this would result in an array with many elements.
-
LJD200 about 8 yearsThanks for the answer. This is very neat. When running it, I receive an exception:
AttributeError: module 'string' has no attribute 'letters'
-
zondo about 8 yearsSorry; I usually code in Python2. In Python3, it was renamed to
ascii_letters
. If you want something that will work in either one, usestring.lowercase + string.uppercase
. -
LJD200 about 8 yearsThis works very well. For Python 3, the brackets need to be added around the
print
function. -
LJD200 about 8 yearsThanks for this answer. How does the code inside the
return
statement work? -
Yoriz about 8 yearsThe split in the previous line removes the first character of the 2nd list item, to replace the character, a new list is created from the first element of the split(index 0) and the character that was split plus the second element of the split(index 1) formatted together.