Bottom Page

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?
Quote
#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

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  counting items in a list of number combinations Dixon 2 126 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Replacing characters in a string with a list cjms981 1 191 Dec-30-2019, 10:50 PM
Last Post: micseydel
  Strange Characters in JSON returned string fioranosnake 4 309 Dec-02-2019, 07:25 PM
Last Post: fioranosnake
  line number of first and second occurance of string in a file mdalireza 1 170 Nov-18-2019, 09:55 AM
Last Post: perfringo
  Split a long string into other strings with no delimiters/characters krewlaz 4 225 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  How can i judge 1st string position is correct number christing 3 262 Oct-30-2019, 03:32 AM
Last Post: newbieAuggie2019
  String + Number from List BollerwagenIng 5 351 Aug-25-2019, 07:47 PM
Last Post: perfringo
  Counting number of occurrences of a single digit in a list python_newbie09 12 700 Aug-12-2019, 01:31 PM
Last Post: perfringo
  TypeError: int() argument must be a string or a number, not 'NoneType' Hadad 2 1,479 Jul-30-2019, 07:22 PM
Last Post: Hadad
  How to iterate over some characters in a string and the others will stay as it is. ? sodmzs 9 628 Jun-17-2019, 06:45 PM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)