1

I wrote a program in which the user may enter any string. It should:

  1. Delete all the vowels.
  2. Insert the character "." before each consonant.
  3. Replaces all uppercase consonants with corresponding lowercase ones.

Here is the code I wrote:

s=raw_input()
k=s.lower()
listaa=[]
for x in k:
    listaa.append(x)
    if x=='a':
    listaa.remove('a')
    if  x=='o':
        listaa.remove('o')
    if x=='y':
        listaa.remove('y')
    if x=='e':
        listaa.remove('e')
    if x=='u':
        listaa.remove('u')
    if x=='i':
        listaa.remove('i')
for a in listaa:
print '.%s'%(a),

This code works fine, but for example if I use the input tour, the output is .t .r. Although this is right, it's not exactly what i want. I want to remove spaces between them. I want output that looks like: .t.r

How do I do this?

Braiam
  • 1
  • 11
  • 47
  • 78
user7179690
  • 1,051
  • 3
  • 17
  • 40
  • 2
    I'm a little confused to why this is getting down votes. Has specs, code, input, expected output, actual output. Probably should be closed because [possible duplicate](http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-newlines-or-spaces) – But I'm Not A Wrapper Class Nov 09 '15 at 20:55
  • This is a valid question. Don't downvote because english isn't perfect. Not everyone is a native english speaker. – Andy Guibert Nov 09 '15 at 20:57

4 Answers4

2

If you put a comma after a print, it adds a space to the print statement. To not print a space or newline, use sys.stdout.write()

Fixed code:

import sys

s=raw_input()
k=s.lower()
listaa=[]
for x in k:
    listaa.append(x)
    if x=='a':
        listaa.remove('a')
    if  x=='o':
        listaa.remove('o')
    if x=='y':
        listaa.remove('y')
    if x=='e':
        listaa.remove('e')
    if x=='u':
        listaa.remove('u')
    if x=='i':
        listaa.remove('i')
for a in listaa:
    sys.stdout.write('.%s' % a)

Note: you will need to add the import sys statement to use sys.stdout.write

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
1

using print elem, in python uses the delimiter of a space which means it automatically prints a space after each call to print. this can be avoided by calling print only once by making a single string:

print ''.join(listaa)

str.join(list) is a built in string method that joins a list's elements together with str. the problem with this is that it wont have a period before each consonant.

your logic can also be changed to remove if statements. best way to do this is to change your logic to a add if not in kind of way:

s=raw_input()
k=s.lower()
listaa=[]
for x in k:
    if x not in ['a','e','i','o','u']: listaa.append('.'+x.lower())

all this does is check if x is a vowel, if not add a string made up of a period and x

you can also do this all in one line:

print ''.join('.'+x.lower() for x in raw_input() if x not in 'aeiou')
R Nar
  • 5,465
  • 1
  • 16
  • 32
1

Avoid using remove, since it removes the first occurrence of an item. Instead, determine what you need to append before appending:

if x in 'bcdfghjklmnpqrstvwxyz':
    listaa.append('.')
    listaa.append(x)
elif x not in 'aeiou':
    # If x is neither a vowel nor a consonant, it is appended.
    listaa.append(x) 

Also, it would be good to convert your list back to a string at the end:

result = ''.join(listaa)
print result
Jonathan Clede
  • 126
  • 1
  • 5
1

Regular expressions (contained in the re library) are designed to do exactly this sort of stuff.

import re
import string

alphabet = string.ascii_lowercase
vowels = 'aeiou'

consonants = "".join(set(alphabet)-(set(vowels)))
vowelre = re.compile("["+vowels+"]", re.IGNORECASE)
consonantre = re.compile("(["+consonants+"]){1}", re.IGNORECASE)

text = "Mary had a little lamb The butcher killed it DEAD"

print(vowelre.sub("", text))
print(consonantre.sub(".", text))

I note that you've put the Python 2.7 tag on your query. If you are using 2.7 then I would recommend you get used to using the Python 3 style print function. To enable this you may need a line

from __future__ import print_function

in your code.

tim@merlin:~$ python retest.py
Mry hd  lttl lmb Th btchr klld t DD
.a.. .a. a .i...e .a.. ..e .u...e. .i..e. i. .EA.

HTH

TimGJ
  • 1,584
  • 2
  • 16
  • 32
  • replace upper-case constants is in OP's requirements i suggest you add string.lower() to result print(vowelre.sub("", text).lower()) – PythonTester Nov 10 '15 at 17:58