w3lcs
w3lcs

Reputation: 13

How do i remove Blank spaces in python?

from selenium import webdriver
import os, time
from selenium.common.exceptions import NoSuchElementException

PATH = ('C:\\Program Files (x86)\\Chrome\\Application\\chromedriver.exe') 
driver = webdriver.Chrome(PATH)
var2 = input('Enter a Link: ')

while True:
    driver.get(var2)
    time.sleep(3)
    driver.refresh()
    time.sleep(3)
    var = driver.find_elements_by_id('practice')
    print(var[0].text)
    try:
        driver.find_element_by_xpath('/html/body/div[9]/section/div[1]/div[1]/div[6]/div/div[1]/div/div[2]/button').click()
    except NoSuchElementException:
        try:
            driver.find_element_by_xpath('/html/body/div[9]/section/div[1]/div[1]/div[6]/div/div[1]/div/div[2]/button').click()
        except NoSuchElementException:
            try:
                driver.find_element_by_xpath('/html/body/div[11]/section/div[1]/div[1]/div[6]/div/div[1]/div/div[2]/button').click()
            except NoSuchElementException:
                print('error go back to main.')

driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/button[2]').click()
time.sleep(1)
myString = driver.find_elements_by_id('bd')[0].text
# clean answer
newString = myString.replace('Got it','')
newStringA = newString.replace('solve','')
newStringB = newStringA.replace('Got it','')
newString1 = newStringB.replace('Questions','')
newString2 = newString1.replace('answered','')
newString3 = newString2.replace('Time','')
newString4 = newString3.replace('elapsed','')
newString5 = newString4.replace('HR MIN SEC','')
newString6 = newString5.replace('SmartScore','')
newString7 = newString6.replace('out of 100','')
newString8 = newString7.replace('SmartScore','')
newString9 = newString8.replace('NEXT PROBLEM','')
newString10 = newString9.replace('Learn with an example','')
newString11 = newString10.replace('You','')
newString12 = newString11.replace('out of','')
newString13 = newString12.replace('So:','Answer:')
newString14 = newString13.replace('review','')
newString15 = newString14.replace('00 00 04','')
newString16 = newString15.replace('The correct answer is:','')
newString17 = newString16.replace('Sorry, incorrect...','')
newString18 = newString17.replace('So:','Answer:')
newString19 = newString18.replace('Submit:','')
newString20 = newString19.replace(':','')


print(newString20)
print('NEXT PROBLEM')
driver.refresh()
driver.delete_all_cookies()

when I print the text, there's a huge gap in between the text and overall tons of white spaces, would there be an efficient and easy way of removing them? for example, there is tons of blank lines here, and i would like to remove them! there is tons of blank lines here, and i would like to remove them?

Upvotes: 0

Views: 1269

Answers (3)

Spartan 117
Spartan 117

Reputation: 129

Why don't you use strip?

#...The most simple example 

txt = "     hamburgers    "

x = txt.strip()

print("I like", x, ".They are my favorites")

The output will be the total string itself but without black spaces. strip eliminates all the indicated blank spaces at the beginning and at the end of the string. You can also use it with other strings

Let's say txt was:

txt = ']hamburger]'
x= txt.strip(']')

If you print x you will get 'hamburger'

Upvotes: 0

EldinAyal
EldinAyal

Reputation: 96

you can always try:

your_string.replace(" ","")

this will remove all white-spaces including leading and trailing white-spaces

Upvotes: 1

shreyas
shreyas

Reputation: 7

You could try removing the ,'' part in every string, since adding '' to your string will just add a blank space to your output.

Upvotes: 0

Related Questions