Python Forum
Functions to remove vowels and extra blanks
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions to remove vowels and extra blanks
#1
Example A:
Input:

Um cidadão ficou sem paciência ao esperar nas Finanças.

O âmbito do atentado foi grande. Ouve 56 vítimas. Que horror!

O coração foi o único órgão afetado, mas o doente não sobreviveu

.

Output:

m cdd fc sm pcnc sprr ns Fnnçs.

mbt d tntd f grnd. v 56 vtms. Q hrrr!

crç f nc rg ftd, ms dnt n sbrvv

Example B:
Input:

Ele já sabia que ia ser assim, tu é que achavas que não

.

Output:

l j sb q sr ssm, t q chvs q n

def isVowel(s):
if s in ('aeiouáéíóúàèìòùãõâêîôûAEIOUÁÉÍÓÚÀÈÌÒÙÃÕÂÊÎÔÛ'):

    return True
else:
    return False
def eraseVowels(cadeia):
    vogais ='aeiouáéíóúàèìòùãõâêîôûAEIOUÁÉÍÓÚÀÈÌÒÙÃÕÂÊÎÔÛ'
    nova_cadeia =''
    for car in cadeia:
        if car in vogais:
            nova_cadeia = nova_cadeia + ''
        else:
            nova_cadeia = nova_cadeia + car
    return nova_cadeia
def eraseExtraBlanks(s):
    while '  ' in s:
        s=s.replace('  ',' ')
        s.strip()

return s.strip()

x = input()
while x != '.':
    x = input()
    if x == '.':
        break
    if ('  ') in x:
        print(eraseExtraBlanks

print(eraseVowels)
I need the program to accept multiple strings until the user enters '.' then is should remove vowels and extra blanks(in case they exist) and show the results aas demonstrated in the examples
Reply
#2
#!/usr/bin/env python3
import sys
def isVowel(s):
    if s in 'aeiouáéíóúàèìòùãõâêîôûAEIOUÁÉÍÓÚÀÈÌÒÙÃÕÂÊÎÔÛ':
        return True
    else:
        return False

def eraseVowels(cadeia):
    nova_cadeia =''
    for car in cadeia:
        if isVowel(car):
            nova_cadeia += ''
        else:
            nova_cadeia += car
    return nova_cadeia

def eraseExtraBlanks(s):
    while '  ' in s:
        s=s.replace('  ',' ')
        s.strip()
    return s.strip()

while True:
    x = input("input: ")
    if x == '.':
        break
    x = eraseExtraBlanks(x)
    x = eraseVowels(x)
    print(x)
Reply
#3
Thank you it is working! But i need something that looks like this:
Example A:
Input:

Um cidadão ficou sem paciência ao esperar nas Finanças.

O âmbito do atentado foi grande. Ouve 56 vítimas. Que horror!

O coração foi o único órgão afetado, mas o doente não sobreviveu

.

Output:

m cdd fc sm pcnc sprr ns Fnnçs.

mbt d tntd f grnd. v 56 vtms. Q hrrr!

crç f nc rg ftd, ms dnt n sbrvv

Reads multiple strings and then gives me the result os them all in a row. Your solution only reads one string and gives me the output straight away.
Reply
#4
I assume you can combine the two programs yourself.

#!/usr/bin/env python3
import sys

def multi_line_input():
    lines = ""
    while True:
        line = input("input: ")
        if line == '.':
            break
        else:
            lines += line + "\n"
    return lines


x = multi_line_input()
print(x)
Reply
#5
That just gives me the string that i wrote or maybe i joined the programs wrong. Can you add that part to your previous answer? Please, that would really be helpfull this is kinda urgent
Reply
#6
Replace
x = input(...)
 
with
x=multi_lines_input()
Reply
#7
Did that! Thanks! Getting this error:
Traceback (most recent call last):
File "C:\Users\Utilizador\Desktop\5.2.py", line 58, in <module>
x = eraseVowels(x)
File "C:\Users\Utilizador\Desktop\5.2.py", line 27, in eraseVowels
for car in cadeia:
TypeError: 'NoneType' object is not iterable
Reply
#8
#!/usr/bin/env python3
import sys

def isVowel(s):
    if s in 'aeiou':
        return True
    else:
        return False

def eraseVowels(cadeia):
    nova_cadeia =''
    for car in cadeia:
        if isVowel(car):
            nova_cadeia += ''
        else:
            nova_cadeia += car
    return nova_cadeia

def eraseExtraBlanks(s):
    while '  ' in s:
        s=s.replace('  ',' ')
    return s.strip()

def multi_line_input():
    lines = ""
    while True:
        line = input("input: ")
        if line == '.':
            break
        else:
            lines += line + "\n"
    return lines

x = multi_line_input()
x = eraseExtraBlanks(x)
x = eraseVowels(x)
print(x)
#done
Reply
#9
Thanks a lot!
Reply
#10
You can reduce the code.

def isVowel(s):
    if s in 'aeiou':
        return True
    else:
        return False
to

def isVowel(s):
    return s.lower() in 'aeiou'
Erasing vowels:

def eraseVowels(cadeia):
    return ''.join(char for char in cadeia if not isVowel(char))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove extra space from output list? longmen 3 1,790 May-05-2022, 11:04 PM
Last Post: longmen
Bug How to print only vowels from an input? PP9044 8 7,558 Feb-26-2021, 04:02 PM
Last Post: Serafim
  Counting Vowels in Python sapphosvoice 1 3,309 May-05-2020, 04:24 AM
Last Post: buran
  Return not vowels list erfanakbari1 2 2,693 Mar-26-2019, 11:37 AM
Last Post: perfringo
  Help with finding vowels nlord7 1 2,266 Feb-27-2019, 04:40 AM
Last Post: ichabod801
  Print the index of the vowels in a string MeeranRizvi 4 14,685 Dec-29-2016, 02:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020