Simsons
Simsons

Reputation: 12745

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?

Upvotes: 6

Views: 14459

Answers (4)

Umang Suthar
Umang Suthar

Reputation: 101

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'

Upvotes: 1

Amber
Amber

Reputation: 527328

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 (e.g. alphanumerics), then the regex solution specified in Tim Pietzcker's solution is probably the easiest way.

Upvotes: 1

Adam Jurczyk
Adam Jurczyk

Reputation: 2131

>>> 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"

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

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

Upvotes: 14

Related Questions