Python: delete all characters before the first letter in a string

After a thorough search, I was able to find how to remove all characters before a particular letter, but not to a letter.

I am trying to rotate a string from this:

"             This is a sentence. #contains symbol and whitespace

For this:

This is a sentence. #No symbols or whitespace

I tried the following code, but lines, such as the first example, still appear.

for ch in ['\"', '[', ']', '*', '_', '-']:
     if ch in sen1:
         sen1 = sen1.replace(ch,"")

This will not only not remove the double quote in the example for some unknown reason, but will also not work to remove the leading space, as it will remove all spaces.

Thanks in advance.

+4
source share
6 answers

Instead of just removing spaces, to remove any char before the first letter, do the following:

#s is your string
for i,x in enumerate(s):
    if x.isalpha()         #True if its a letter
    pos = i                   #first letter position
    break

new_str = s[pos:]
+1
source

You can use re.sub

import re
text = "             This is a sentence. #contains symbol and whitespace"

re.sub("[^a-zA-Z]+", " ", text)

re.sub (MATCH PATTERN, REPLACE STRING, STRING TO SEARCH)

0

; .. , Python.

your_string = "1324 $$ '!'     '' # this is a sentence."
while len(your_string) > 0 and your_string[0] not in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
    your_string = your_string[1:]
print(your_string)

#prints "this is a sentence."

: ,

Cons: a while loop could be avoided if you are more comfortable using lists. Also, the string you are comparing may be simpler using a regular expression.

0
source

Separate all spaces and punctuation marks:

>>> text.lstrip(string.punctuation + string.whitespace)
'This is a sentence. #contains symbol and whitespace'

Or, alternatively, find the first character, which is the letter ascii. For instance:

>>> pos = next(i for i, x in enumerate(text) if x in string.ascii_letters)
>>> text[pos:]
'This is a sentence. #contains symbol and whitespace'
0
source

Drop everything to the first alpha character.

import itertools as it


s = "      -  .] *    This is a sentence. #contains symbol and whitespace"
"".join(it.dropwhile(lambda x: not x.isalpha(), s))
# 'This is a sentence. #contains symbol and whitespace'

Alternatively, iterate over the string and check if each character is blacklisted. If the true character strip, otherwise a short circuit.

def lstrip(s, blacklist=" "):    
    for c in s:
        if c in blacklist:
            s = s.lstrip(c)
            continue
        return s

lstrip(s, blacklist='\"[]*_-. ')
# 'This is a sentence. #contains symbol and whitespace'
0
source
import re
s = "  sthis is a sentence"

r = re.compile(r'.*?([a-zA-Z].*)')

print r.findall(s)[0]
0
source

Source: https://habr.com/ru/post/1686086/


All Articles