我需要显示多个单词的第一个字母,这些单词是从列表中的多个项目中收集的
尝试收集列表项,使用word.split(" ")
和song[0][0/1]
分隔它们
for word in song:
songWords = word.split(" ")
songLetter1 = songWords[0][0]
songLetter2 = songWords[1][0]
print("")
print("The artist is", artist, "and the first letter of the song is",
songWord1, songWord2)
希望“ s”和一个数字,具体取决于随机选择的项目
答案 0 :(得分:0)
从.txt文件中读取文本后,可以将其存储在字符串中。我假设该字符串看起来像下面的songText。
尝试:
songText = "song 1, song 2"
songs = songText.split(",")
print(songs)
artist = "artist 1"
for song in songs:
song = song.strip() # To remove spaces before and after the text.
songWords = song.split(" ")
# The below lines will work assuming there are at least two words in the song.
songLetter1 = songWords[0][0]
songLetter2 = songWords[1][0]
print("")
print("The artist is", artist, "and the first letter of the song is",
songLetter1, songLetter2)
希望这会有所帮助!
答案 1 :(得分:0)
如果输入是列表
song=["hey","lol"]
for word in song:
songWords = word.split(" ")
songWord1 = songWords[0][0]
print("")
print("The artist is and the first letter of the song is",songWord1)