Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing any single letter on a string in python

I would like to remove any single letter from a string in python.

For example:

input: 'z 23rwqw a 34qf34 h 343fsdfd'
output: '23rwqw 34qf34 343fsdfd'

Been trying to figure out for a while with regex without success. I know how to cut things from certain char/symbol in two pieces, but not what I am trying to do.

I have been tinkering with

re.sub(r'^[^ ]*', '', text)
like image 876
KubiK888 Avatar asked Sep 21 '15 23:09

KubiK888


People also ask

How do I remove a single letter from a string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How do I remove characters from a string in Python?

Use the . strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.


1 Answers

>>> ' '.join( [w for w in input.split() if len(w)>1] )
'23rwqw 34qf34 343fsdfd'
like image 104
Sait Avatar answered Sep 18 '22 00:09

Sait