hello,

pls, can anybody help?

I try to print words which contains forbidden letters but counting without them and excluding the words which has the smallest number of letters.

example:

words = 'hello this is my test'
forbidden = 'h, t, e'

1. hello this test -> these word contains forbidden letters

2. if I exclude the forbidden letters from these words, I will get -> llo is s
(it's important that 'llo' 'is' 's' corresponts to one separate string)
3. finaly I will print the words -> hello this
(excluding test because it contains just one letter s)

point first it's done
point two -> I need help
point three -> not started yet

point first:

def small():
    words = 'hello, this is my test'
    forbidden = 'l, h, e'
    for word in words.split():
        for letter in word:
            if letter in forbidden:
                print word
                break
            continue

small()

point two: I did something but problem is that string is together which will be problem in point three. I need to have it separately.

def small():
    words = 'hello, this is my test'
    forbidden = 'l, h, e'
    a = ''
    for word in words.split():
        for letter in word:
            if letter in forbidden:
                letter = ''
            else:
                a = a + letter


    print a,

small()

Recommended Answers

All 2 Replies

First of all, I would use a Python string function 'translate'... then a 'split' with a space as argument... an example in the Python interpreter...

>>> mystr = "Hello, test"
>>> newstr = mystr.translate(None, 'les')
>>> print newstr
Ho, tt
>>> splitstr = newstr.split(' ')
>>> print splitstr
['Ho,', 'tt']
>>>

You need to add a space between words:

def small():
    words = 'hello, this is my test'
    forbidden = 'l, h, e'
    a = ''
    for word in words.split():
        for letter in word:
            if letter in forbidden:
                letter = ''
            else:
                a = a + letter
        # add a space between words
        a = a + ' '

    print a

small()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.