Function for removing vowels from a character - python-3.x

Define a function called anti_vowel that takes one string, text, as input and returns the text with all of the vowels removed.
For example, anti_vowel("Hey You!") should return "Hy Y!". Don't count Y as a vowel. Make sure to remove lowercase and uppercase vowels.
def anti_vowel(text):
b=""
for i in text:
print i
for j in "aeiouAEIOU":
if i!=j:
b+=i
break
return b
what am I doing wrong?

You should check using in.
If i not in "aeiouAEIOU":

Related

Separating a string with large letters into words that begin with the same letters

Suppose you have a string "TodayIsABeautifulDay". How can we get separate it in Python into words like this ["Today", "Is", "A", "Beautiful", "Day"]?
First, use an empty list ‘words’ and append the first letter of ‘word’ to it.
Now using a for loop, check if the current character is in lower case or not, if yes append it to the current string, otherwise, if uppercase, begin a new individual string.
def split_words(word):
words = [[word[0]]]
for char in word[1:]:
if words[-1][-1].islower() and char.isupper():
words.append(list(char))
else:
words[-1].append(char)
return [''.join(word) for word in words]
You can use this function :
word = "TodayIsABeautifulDay"
print(split_words(word))

Write a function that removes all occurrences of a given letter from a string [duplicate]

This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 3 years ago.
Write a function that removes all occurrences of a given letter from a string:
remove_letter("a", "apple") == "pple"
remove_letter("a", "banana") == "bnn"
remove_letter("z", "banana") == "banana"
I have tried to make a list from the string and then deleting the element. Below is my code for this problem. Is this correct? Or should I think of something else? Can you tell a better method than this and why is that method better?
def rmv(word, letter):
word = list(word)
for ele in word:
if ele == letter:
word.remove(ele)
print(word)
This code does not show any error messages. It gives the expected output for the words like "banana" but for word like "bananna" it would not give the expected output.
For rmv("banana", "n") output is "baaa".
For rmv("bananna", "n") output is "baana".
Your code approach with fix:
def rmv(word, letter):
word = list(word)
newWord=[]
for i in range(len(word)):
if word[i]!= letter:
newWord.append(word[i])
print(newWord)
rmv("bananna", "n")
What you were doing wrong:
You were iterating your list and deleting as and when your if-else met the condition:
if ele == letter:
word.remove(ele)
But the problem with this approach is that your pointer moves ahead and the deletion causes the list to shift accordingly at the same time and so when you see the case of
rmv("bananna", "n")
By the time pointer could get the opportunity to scan and remove the last n the list caused it to shift at an index that was already scanned by your list and hence it got ignored.
On a side note -
I generally advise people to use Python-tutor to debug their code. It's GUI based code visualizations helps to know where you went wrong.
Your problem is that you modify (remove) a list while you iterate over it. Just put a few print lines into your code to see what it does.
It's better to create a new list and adding only characters that you want to keep:
def rmv(word, letter):
out = []
for char in word:
if char != letter:
out.append(char)
return ''.join(out)
or, even more compactly:
def rmv(word, letter):
return ''.join(char for char in word if char != letter)
Why not just:
def rmv(word, letter):
return word.replace(letter, '')
print(rmv("apple", "a"))
There is no need for you to iterate through each character of the string when one function call will do it for you.
Why don't you try the string replace function?
Try this code:
def rmv(word, letter):
return word.replace(letter, "")

i can't understand what to use in this code(+= or .join())

so the question asks to return a string in which every character of the original string are 3 times i.e if the word='hello'
then the string should return 'hhheeelllooo'
(python code)
i tried using string.join in this case but it only returns 'hhh' but if i use string+=character*3 it works .so what is the differense and why isn't it working
def myfunc (text):
string=''
for letter in text:
return string.join(letter*3)
but
def myfunc (text):
string=''
for letter in text:
string+=letter*3
return string
it works
for letter in text:
return string.join(letter*3) # return at first letter,usel
it will work
''.join([i*3 for i in text])
When you write return string.join(letter*3) (even though it's not what you want) it will exit the function without finishing the loop, thus returning the result for only the first letter. Also, string.join(X) works with sequences such as lists, and it glues the elements in the list X with the string string, so if you want to use it you can use it this way:
def myfunc(text):
return ''.join([letter * 3 for letter in text])
print(myfunc('hello'))
Output:
hhheeellllllooo

converting individual characters in string to uppercase

my code
name='helloworld'
i=0
while(i<len(name)):
name[i].upper()
i=i+2
print(i)
print(name)
want the output as 'AnThRoPoMoRpHiSm'. Please help.
You can enumerate your string and capitalize one of every 2 letters, then join the result:
name='anthropomorphism'
''.join([s if i%2 else s.upper() for i,s in enumerate(name)])
This returns:
'AnThRoPoMoRpHiSm'

How to keep the vowel at the beginning of all the words in a string, but remove in the rest of the string

def shortenPlus(s) -> "s without some vowels":
for char in s:
if char in "AEIOUaeiou":
return(s.replace(char,""))
I have the taken it out of the entire string. But I can't figure out how to restrict the replace function to everything but the first letter of each word in a string.
Not sure exactly what you're looking for, can you clarify, perhaps give a simple example? None of the words you have in your example start with vowels!
But here you could remove all the vowels in a word except the first vowel of the first word. Hard coded but gives you an idea:
s="without some vowels"
for char in s[2:]:
if char in "AEIOUaeiou":
s=s.replace(char,"")
print(s)
Outputs
witht sm vwls
Alternatively, to get the first char of every word, you could use a sentinel value that flags each time a non-alpha char such as punctuation or a space is present, then keeps the next char but not the others.
s="without some vowels"
sent=2
for char in s:
if sent>0:
sent-=1
print(char)
continue
if not char.isalpha():
sent=2
continue
s=s.replace(char,"")
print(output)
Outputs
w s v
def shortenPlus(s):
counter = 0 # accepted character count
add_the_vowel = True # check if vowel came for the first time for the word
temp = " " # temp string to store the output
for letter in s:
if letter == " ":
add_the_vowel= True
if add_the_vowel == True and letter in "AEIOUaeiou":
temp += s[counter] # first vowel of the word
if letter in "AEIOUaeiou":
add_the_vowel = False # restrict second vowel appeared
else:
temp += s[counter]
counter += 1
print(temp)
s = "without some vowels frienis"
shortenPlus(s)
How to keep the vowel at the beginning of all the words in a string, but remove in the rest of the string
output :
witht som vowls frins

Resources