3

As a beginner to Python I got these tasks by the teacher to finish and I'm stuck on one of them. It's about finding the consonants in a word using a for-loop and then create a string with those consonants.

The code I have is:

consonants = ["qwrtpsdfghjklzxcvbnm"]
summer_word = "icecream"

new_word = ""

for consonants in summer_word:
    new_word += consonants

ANSWER = new_word

The for-loop I get but it's the concatenation I don't really get. If I use new_word = [] it becomes a list, so I should use the ""? It should become a string if you concatenate a number of strings or characters, right? If you have an int you have to use str(int) in order to concatenate that as well. But, how do I create this string of consonants? I think my code is sound but it doesn't play out.

Regards

5 Answers 5

5

Your loop is currently just looping through the characters of summer_word. The name "consonants" you give in "for consonants..." is just a dummy variable, it doesn't actually reference consonants that you defined. Try something like this:

consonants = "qwrtpsdfghjklzxcvbnm" # This is fine don't need a list of a string.
summer_word = "icecream"

new_word = ""

for character in summer_word: # loop through each character in summer_word
    if character in consonants: # check whether the character is in the consonants list
        new_word += character
    else:
        continue # Not really necessary by adds structure. Just says do nothing if it isn't a consonant.

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

Comments

1

A string in Python is already a list of characters and may be treated as such:

In [3]: consonants = "qwrtpsdfghjklzxcvbnm"

In [4]: summer_word = "icecream"

In [5]: new_word = ""

In [6]: for i in summer_word:
   ...:     if i in consonants:
   ...:         new_word += i
   ...:

In [7]: new_word
Out[7]: 'ccrm'

Comments

1

You are right, if the character is a number you must use str(int) to convert it in string type.

consonants = ["qwrtpsdfghjklzxcvbnm"]
summer_word = "icecream"

new_word = ""
vowels = 'aeiou'
for consonants in summer_word:
    if consonants.lower() not in vowels and type(consonants) != int:
        new_word += consonants
answer = new_word

Here inside the for loop you are evaluating if the 'consonants' is not a vowel and is not an int. Hope this help you.

Comments

0

The issue you have here is that you have created the variable consonants as a list with a string in it. So remove the square brackets and it should work

Comments

0
consonants = "qwrtpsdfghjklzxcvbnm"
summer_word = "icecream"

new_word = ""


for letter in summer_word:
    if letter in consonants:
      new_word += letter

print(new_word)

A shorter one would be

consonants = "qwrtpsdfghjklzxcvbnm"
summer_word = "icecream"

new_word = ""

new_word = [l for l in summer_word if l in consonants]
print("".join(new_word))

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.