Counting chars in a file | python 3x
Question: I'm wondering, how can I count for example all "s" characters and print their
number in a text file that I'm importing? Tried few times to do it by my own
but I'm still doing something wrong. If someone could give me some tips I
would really appreciate that :)


Answer: Open the file, the `"r"` means it is opened as readonly mode.

    
    
    filetoread = open("./filename.txt", "r")
    

With this loop, you iterate over all the lines in the file and counts the
number of times the character _chartosearch_ appears. Finally, the value is
printed.

    
    
    total = 0
    chartosearch = 's'
    for line in filetoread:
        total += line.count(chartosearch)
    print("Number of " + chartosearch + ": " + total)
    

