About 14,700,000 results
Open links in new tab
  1. python - counting the word length in a file - Stack Overflow

    Feb 4, 2011 · occurrence = dict() for word in words: try: occurrence[len(word)] = occurrence[len(word)] + 1 except KeyError: occurrence[len(word)] = 1 print occurrence.items() …

  2. Count Words in Text File in Python - GeeksforGeeks

    Mar 25, 2025 · Our task is to create a Python program that reads a text file, counts the number of words in the file and prints the word count. This can be done by opening the file, reading its …

  3. How to Perform Word Count in Python Program - Python Guides

    Apr 12, 2024 · Similarly, you can count the words of any file by opening the file in read mode, then reading the data of the opened file using the read(), splitting its content into words using the …

  4. Python: Count Words in a String or File - datagy

    Apr 4, 2022 · In this tutorial, you’ll learn how to use Python to count the number of words and word frequencies in both a string and a text file. Being able to count words and word …

  5. Using the len() Function in Python – Real Python

    Nov 16, 2024 · To find the length of a list in Python, you pass the list to len(), like len([1, 2, 3]). The len() function operates in constant time, O (1), as it accesses a length attribute in most …

  6. How to find length of words in a file in Python - Stack Overflow

    Feb 21, 2016 · Files are iterable, so once you open a file, you can iterate through lines as for line in file:, splitting by words is done as line.split(), word length is len(word). You could use …

  7. Python - Count occurrences of each word in given text file

    Aug 25, 2022 · Many times it is required to count the occurrence of each word in a text file. To achieve so, we make use of a dictionary object that stores the word as the key and its count as …

  8. python - how to find the length of the file in python3 ... - Stack Overflow

    Mar 20, 2019 · input_file = open(file_name, 'r') for line in input_file.readlines(): print(line) If you want the number of lines in the file, do the following. lines_in_file = open(file_name, …

  9. python - How to get the first word in the string - Stack Overflow

    Just use some_string.split(' ', 1)[0] or some_string.partition(' ')[0]. Not if the words are separated by other characters (e.g. tabs). As long as they are separated by the same character, it will …

  10. python - I want to find the length of my each words in text file ...

    Apr 21, 2020 · If you want to count the length of the word, you could simply do. for word in words: if word not in d: d[word] = len(word) And to output your dict, you can do. for k, v in d.items(): …

Refresh