0

I'm trying to teach myself Python and am doing some menial tasks with it. At the minute I'm working with lists and strings. I know that strings are immutable so I'm converting a string to a list and want to loop over the list to change any vowels to $ signs. The problem is that the $ sign isn't being attributed to the vowels. Here is my code:

aString = raw_input("Please enter a sentence: ")

aString = list(aString)

for i in xrange(len(aString)):
    if i=='a' or \
       i=='e' or \
       i=='i' or \
       i=='o' or \
       i=='u':
        i.newattribute = '$'

print aString
2
  • xrange generates integers, rather than characters :-) Commented May 11, 2012 at 16:17
  • @MaksymPolshcha Thanks for that. Have to do a bit more research into loops etc in Python. There's a lot to them Commented May 11, 2012 at 16:20

5 Answers 5

4

I know you are doing it to learn the language, but you should know you could simply use the method sub to substitute with a regular expression:

import re
re.sub('[aeiou]', '$', aString)
Sign up to request clarification or add additional context in comments.

Comments

2

You want to do the following:

for i in xrange(len(aString)):
    if aString[i]=='a' or \
       aString[i]=='e' or \
       aString[i]=='i' or \
       aString[i]=='o' or \
       aString[i]=='u':
          aString[i] = '$'

But it would probably be easier to use the replace method.

replaceList = ['a', 'e', 'i', 'o', 'u']
aString = raw_input("Please enter a sentence: ")
for letter in replaceList:
    aString.replace(letter)
print aString

1 Comment

The replace function is something that I haven't seen before. Thanks for that Niek. Python is full of these new functions that I haven't seen before. I'm only familiar with C and C++
2
strs="abcduitryd"
print("".join(('$' if x in 'aeiou' else x for x in strs)))

$bcd$$tryd

or:

strs="abcduitryd"
lis=list(strs)
for i,x in enumerate(lis):
    if x in 'aeiou':
        lis[i]='$'
strs="".join(lis)
print(strs)

$bcd$$tryd

or :

strs="abcduitryd"
for i,x in enumerate(strs):
    if x in 'aeiou':
        strs=strs.replace(strs[i],'$')
print(strs)

$bcd$$tryd

2 Comments

you don't have to list() the string. a string is an iterable.
oh! i forgot to remove that line, when I copied this from the IDE.
1

The "Pythonic" way is to use the translate method for string.
example:

import string
mystr="hello world"
vowels="aeiou"
token="$"*len(vowels)
tab=string.maketrans(vowels,token)
mystr.translate(tab)
'h$ll$ w$rld'

Comments

1

If you are learning python, one cool feature that can be used is a list comprehension. You can do this:

>>> str = "hello world"
>>> l = ["$" if ch in "aeiou" else ch for ch in str]
>>> str = "".join(l)
>>> str
'h$ll$ w$rld'

The second line builds a list, walking through each character and applying `"$" if ch in "aeiou" else ch1 to it. You then just join the list to get a new string. It's doing exactly what you are trying to do, converting the string to a list and in the process, coverting vowels to '$'.

This is not the most efficient way to do it of course. The best way to do it is to use a library meant for this sort of thing as others have mentioned.

1 Comment

You don't really need to make a separate list, you can just change that to a generator expression and put it right inside "".join().

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.