0

I'm new to Python. Trying to make a simple function that converts a string input to braille via dict values (with '1' indicating a bump and 0 no bump).

I'm sure there are faster/better ways to do this but what I have is almost working (another way of saying it doesn't work at all).

alphabet = {
'a': '100000','b': '110000','c': '100100','d': '100110'
#etc
}
def answer(plaintext):

    for i in str(plaintext):
        for key, value in alphabet.iteritems():
            if i in key:
                print value,            

answer('Kiwi')

This prints:

000001101000 010100 010111 010100

My question is how do I remove the spaces? I need it to print as:

000001101000010100010111010100

It's printing as a tuple so I can't use .strip().

Cecilia
  • 4,512
  • 3
  • 32
  • 75
Stu
  • 17
  • 5
  • 1
    http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-newlines-or-spaces – user1952500 Mar 20 '17 at 22:55
  • Possible duplicate of [How do I keep Python print from adding newlines or spaces?](http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-newlines-or-spaces) – Cecilia Mar 20 '17 at 22:58

5 Answers5

1

For what I'd consider a pythonic way:

def answer(plaintext):
    alphabet = { ... }
    return "".join([alphabet[c] for c in plaintext])

(this assumes that all letters in plaintext are in alphabet)

marisbest2
  • 1,346
  • 2
  • 17
  • 30
  • Thanks so much! This works perfectly. I understand the looping through each c in plaintext, and that the dict values are being joined without spaces, but can you briefly explain how the rest works - specifically the alphabet[c] ? – Stu Mar 20 '17 at 23:31
  • `alphabet` is a dict, so `alphabet[c]` is the encoding for that letter in the plaintext – marisbest2 Mar 21 '17 at 17:36
  • I understand. Thanks again. – Stu Mar 21 '17 at 21:06
0
brail = []
for i in str(...):
  if alphabet.get(i):
    brail.append(alphabet[i])
print ''.join(brail)
Dan
  • 1,874
  • 1
  • 16
  • 21
0

In the first line of the function, create a container l=[] .Change the last statement inside the loops to l.append(value).Outside the loops,but still inside the function, do return ''.join(l). Should work now.

Mikael
  • 482
  • 8
  • 23
0

You can use join() within a generator like this way:

alphabet = {'a': '100000','b': '110000','c': '100100','d': '100110'}

def answer(a = ''):
    for i in a:
        for key, value in alphabet.iteritems():
            if i in key:
                yield value

print ''.join(answer('abdaac'))

Output:

>>> 100000110000100110100000100000100100
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
0

One way to manipulate printing in Python is to use the end parameter. By default it is a newline character.

print("foo")
print("bar")

will print like this:

foo
bar

In order to make these two statements print on the same line, we can do this:

print("foo",end='')
print("bar",end='')

This will print like this:

foobar

Hopefully this is a helpful way to solve problems such as this.

Resin Drake
  • 528
  • 4
  • 9