Bhaskaran
Bhaskaran

Reputation: 11

Python: Counting words from a given file starting with 'L'

I am new to python.I want to know how to count the number of words starting with a particular letter say 'L' from a text file.

Upvotes: 0

Views: 1151

Answers (1)

code_bash
code_bash

Reputation: 701

str.startswith(prefix[, start[, end]])

Give this a shot but import your file there are also a few other ways.

list = ["apple", "bannana", "custard", "shoe", "ant", "police", "python"]
newList = []

for word in list:
    if word.startswith('a'):
        newList.append(word)

print newList
['apple', 'ant']

Upvotes: 1

Related Questions