简体   繁体   English

Python-使用for循环创建字符串

[英]Python - Create string with for-loop

As a beginner to Python I got these tasks by the teacher to finish and I'm stuck on one of them. 作为Python的初学者,我让老师完成了这些任务,而我被其中一项困住了。 It's about finding the consonants in a word using a for-loop and then create a string with those consonants. 这是关于使用for循环在单词中查找辅音,然后使用这些辅音创建一个字符串。

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. 我得到的for循环,但这是我没有真正得到的串联。 If I use new_word = [] it becomes a list, so I should use the "" ? 如果我使用new_word = []它将成为一个列表,因此我应该使用"" 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. 如果您有一个int,则还必须使用str(int)来进行连接。 But, how do I create this string of consonants? 但是,如何创建这串辅音呢? I think my code is sound but it doesn't play out. 我认为我的代码是正确的,但无法播放。

Regards 问候

Your loop is currently just looping through the characters of summer_word. 您的循环当前仅循环遍历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

A string in Python is already a list of characters and may be treated as such: Python中的字符串已经是一个字符列表,可以这样对待:

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'

You are right, if the character is a number you must use str(int) to convert it in string type. 没错,如果字符是数字,则必须使用str(int)将其转换为字符串类型。

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. 在for循环中,您正在评估“辅音”不是元音也不是int。 Hope this help you. 希望这对您有所帮助。

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 因此,删除方括号,它应该可以工作

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM