Techiio-author
Started by Anik AdhikaryMay 14, 2022

Open
How to write a python code for find the “total number of strings found in a file” ?

5 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 5 VIEWES SHARE

I am stuck at creating a code to find the “total number of strings found in a file”. I underderstand how code something to find the total number of characters/numbers etc. please someone help me for the code.............

1 Replies

Techiio-commentatorSidhdarth Basu replied 3 months ago0 likes0 dislikes

Syntax of String count

The syntax of count() method is:

string.count(substring, start=..., end=...)

count() Parameters

count() method only requires a single parameter for execution. However, it also has two optional parameters:

substring - string whose count is to be found.

start (Optional) - starting index within the string where search starts.

end (Optional) - ending index within the string where search ends.

Note: Index in Python starts from 0, not 1.

count() Return Value

count() method returns the number of occurrences of the substring in the given string.

Example 1: Count number of occurrences of a given substring

# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count(substring)
# print count
print("The count is:", count)
Run Code
Output:
The count is: 2

Example 2: Count number of occurrences of a given substring using start and end

# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count(substring, 8, 25)
# print count
print("The count is:", count)
Run Code
Output
The count is: 1

Here, the counting starts after the first i has been encountered, i.e. 7th index position.

And, it ends before the last i, i.e. 25th index position.

You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance