Python Forum
Counting number of characters in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting number of characters in a string
#1
I’m trying to count the number of words that exist in the last line of a small text file.

Here is my text file:
Quote:Welcome to your First Tribulation Recruit.
Only the best recruits can become agents.
Do you have what it takes?
We will test your knowledge with this field readiness tribulation.
It should be pretty simple, since you only know the basics so far.
Let's get started.
Best of luck recruit.

Here is my code:
with open('field.txt') as field_variable:
    field_variable = field_variable.readlines() 
    list(field_variable)
    last = field_variable[-1] 
    num_chars = last.count(substring, start=0,end=-1) 
    print(num_chars)
Here is my expected output:
Quote:4

What I actually get is a traceback:
Quote:---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-21-5069a38ae83b> in <module>()
3 list(field_variable)
4 last = field_variable[-1]
----> 5 num_chars = last.count(substring, start=0,end=-1)
6 print(num_chars)

NameError: name 'substring' is not defined

As the traceback indicates, the issue is the form of my count method. It requires a substring as an argument. On Google I found a few tutorials (one, two) on Python’s count method which I learned is typically used to count the number of instances of a word or phrase inside a given string. I have my string declared but I do not wish to count the times a certain word appears inside it. Instead I wish to count the number of words for the whole line. As you can see at line 5 in my code, it says ‘substring’ not defined. If I remove the undeclared substring parameter, I then get this:
Quote:TypeError: count() takes no keyword arguments
What should my first keyword argument be in my count method to determine and then print the number of words in the last line of my text file?
Reply
#2
The count method returns the number of times a specific string exists in another string. You have to give it the string to count. For example, "Craig Ichabod O'Brien".count('a') is equal to 2.

If you want to count the number of words in a line, use the split method. That will split the text into a list of words (words being text with no white space). The len of that list will be the number of words.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,482 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Question Extracting Version Number from a String britesc 2 1,087 May-31-2023, 10:20 AM
Last Post: britesc
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,521 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,849 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Find if chain of characters or number Frankduc 4 1,796 Feb-11-2022, 01:55 PM
Last Post: Frankduc
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,202 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,192 May-02-2021, 03:45 PM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 3,497 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 1,665 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Extract continuous numeric characters from a string in Python Robotguy 2 2,630 Jan-16-2021, 12:44 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020