1

I'm attempting to find all vowels within a string to replace them.

Here is what I've been working with:

word = "abcde"
vowels = "aeiou"
v = list(vowels)
hey = False
for i in range(len(word)):
    if word[i] == v:
        hey = True
print hey

I was trying to replace all those positions with strings with the symbol "$", but I can't figure out how I can do this linear search properly.

0

5 Answers 5

3

Under the assumption that this is for an assignment/class of some sort, here is a simple example. You can iterate through a string by character, so this goes through each letter in your vowel set and replaces each instance in your word with the $ character:

In [33]: s = 'abcde'

In [34]: for c in 'aeiou':
   ....:     s = s.replace(c, '$')
   ....:
   ....:

In [35]: s
Out[35]: '$bcd$'

And keeping it simple, to do it in reverse:

In [6]: s = 'abcde'

In [7]: replace = ''

In [8]: for c in s:
   ...:     if c not in 'aeiou':
   ...:         replace += c
   ...:
   ...:

In [9]: for c in replace:
   ...:     s = s.replace(c, '$')
   ...:
   ...:

In [10]: s
Out[10]: 'a$$$e'

This doesn't get into a lot of other very cool functions which can handle this in one/two lines, but hopefully will serve as a building block :)

Sign up to request clarification or add additional context in comments.

8 Comments

What if I wanted to do this backwards, for example return 'a$$$e'?
@Flow You could do the reverse. Keeping it simple, you could make a string called replace = ''. Then you could do a loop through your string s, checking each string to see if it was in your list of vowels, and if so, appending that string to s. You could then do the same process as in the answer above, but instead of for c in 'aeiou', you could do for c in replace.
@Flow I'm also assuming this is for an exercise of some sort where you need to use some basic methods, so hopefully this works :)
You could use a set to do this for you - from string import ascii_lowercase; replace = set('aeiou').symmetric_difference(ascii_lowercase) - But using the existing code, this will loop the string 21 times, instead of 5...
@JonClements Yeah, I was more looking at this in terms of a class assignment so kept it as basic as possible from a conceptual standpoint. I would approach it a bit differently if I were to do it myself :)
|
3

Using a regular expression is probably the easiest:

(?i) means do case insensitive comparisons
[aeiou] means any of a, e, i, o, or u

The rest is fairly obvious

import re

s = 'alpha beta charlie delta echo foxtrot golf hotel'
print re.sub('(?i)[aeiou]', '$', s)
# $lph$ b$t$ ch$rl$$ d$lt$ $ch$ f$xtr$t g$lf h$t$l

Either that, or str.translate:

from string import maketrans

to_dollar = 'aeiouAEIOU'
trans = maketrans(to_dollar, '$' * len(to_dollar))
print s.translate(trans)
# $lph$ b$t$ ch$rl$$ d$lt$ $ch$ f$xtr$t g$lf h$t$l

Either that, or using a dict:

lookup = dict.fromkeys('aeiouAEIOU', '$')
print ''.join(lookup.get(c, c) for c in s)
# $lph$ b$t$ ch$rl$$ d$lt$ $ch$ f$xtr$t g$lf h$t$l

3 Comments

@zero hehe, thanks - not quite sure how I deleted that on pasting!
a.get(c, c) is a very handy idiom for replacing some terms in a sequence and leaving the rest alone, and works even outside the string context.
+1 - str.translate and itertools are probably my favorite things that I've learned on this site.
3
"".join(("$" if char in vowels else char) for char in string)

Comments

1

You can use a set to quickly determine if the character you are iterating over is a vowel. The set object (usually) has constant look-up time instead of linear look-up time like a list.

vowels = set(vowels)
''.join('$' if ch in vowels else ch for ch in string)

1 Comment

True -- but since it has size 5, it doesn't really matter.
0
word = "a quick brown fox"
vowels = list("aeiou")
hey = False
for w in word:
    hey = hey or w in vowels

print hey

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.