pythonstringlistsplitcharactercount

how do I separate a text with a list?


this is my code however it keeps outputting the answer as one while I want it to count the characters in the sentence.

#-----------------------------
myList = []
characterCount = 0
#-----------------------------

Sentence = "hello world"
newSentence = Sentence.split(",")
myList.append(newSentence)
print(myList)
for character in myList:
    characterCount += 1
print (characterCount)

thank you for your help


Solution

  •  The one line solution

    len(list("hello world"))  # output 11
    

    or...

     Quick fix to your original code

    Revised code:

    #-----------------------------
    myList = []
    characterCount = 0
    #-----------------------------
    
    Sentence = "hello world"
    myList = list(Sentence)
    print(myList)
    for character in myList:
        characterCount += 1
    print (characterCount)
    

    Output:

    ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
    11