简体   繁体   中英

How to remove special characters from the beginning of a string in Python

I am getting my data from XML which may some time contain special Character at beginning like:

'This is a sample title or %&*I don't know if this is the text

I tried with : title[0].isstring() or title[0].isdigit() and then remove the character. But if there are more than one special character at the beginning, then how do I remove it? Do I need a for loop?

You could use a regular expression:

import re
mystring = re.sub(r"^\W+", "", mystring)

This removes all non-alphanumeric characters from the start of your string:

Explanation:

^   # Start of string
\W+ # One or more non-alphanumeric characters
>>> import re
>>> re.sub(r'^\W*', '', "%&*I don't know if this is the text")
"I don't know if this is the text"

#or

>>> "%&*I don't know if this is the text".lstrip("!@#$%^&*()")
"I don't know if this is the text"

If there are just a few specific kinds of characters you want to remove, use lstrip() ("left strip").

For instance, if you wanted to remove any starting % , & , or * characters, you'd use:

actual_title = title.lstrip("%&*")

On the other hand, if you want to remove any characters that aren't part of a certain set (eg alphanumerics), then the regex solution specified in Tim Pietzcker's solution is probably the easiest way.

Using a strip function to remove any special characters from the beginning and end of the string. Ex.

 str = ").* this is text .(" str.strip(")(.* ") Output: 'this is text' 

If you want to remove from the beginning of string use lstrip() Ex.

 str = ").* this is text .(" str.lstrip(")(.* ") Output: 'this is text .(' 

If you want to remove from the end of the string use rstrip() Ex.

 str = ").* this is text .(" str.rstrip(")(.* ") Output: ').* this is text' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM