Home > other >  Unable to remove first character from a string in Python 3
Unable to remove first character from a string in Python 3

Time:03-14

I'm using Beautiful Soup to crawl the data from a website.

# Get the price
# productPrice = "¥249.00"
productPrice = soup.find('span', class_='price').text # this line returns a string ¥249.00 
currPrice = productPrice.lstrip("¥") # remove currency sign
print(currPrice) 
print(type(currPrice)) 

The above code is not removing the first character, the output is:

¥249.00
<class 'str'>

However, if I switch to use local variable and try to remove the first character, it works fine

# Get the price
productPrice = "¥249.00"
# productPrice = soup.find('span', class_='price').text # this line returns a string ¥249.00 
currPrice = productPrice.lstrip("¥") # remove currency sign
print(currPrice) 
print(type(currPrice))   

The above code output is:

249.00
<class 'str'>

I tried using slicing like: currPrice = productPrice[1:] but was still not able to remove the first character. What can be the issue here?

CodePudding user response:

Just in case the pattern is still the same and you only like to get the float like value you can split() by currency sign and strip() the last element in ResultSet:

productPrice='\n¥249.00 '
currPrice = productPrice.split('¥')[-1].strip()

#output
#249.00

Note: Output is still a string until you converted it to a real float -> currPrice = float(currPrice)

  • Related