• Register
2 votes
40 views

Problem:

Hello good soul,

I have passed a successful year in java programming and now started learning python very recently. In java, I used the array.length function to find out the string length. What is the best practice in python to count a String length?

Thanks for your answer.
I really appreciate your effort.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

Hello Gavin,

I am aware that you have a strong command of programming as per your saying. In python, we can do the same thing in different ways.

I am going to introduce you to the function count().  It is the most convenient method in Python to get the occurrence of any element in any container. This is easy to code and remember and hence quite popular.

# Python3 code to demonstrate  
# occurrence frequency using  

# initializing string  
my_str = "I Love Python"
  
# using count() to get count  
counter = my_str.count('o') 
  
# printing result  
print ("Count of o in I Love Python is : "
                           +  str(counter))

The above program will look for the ‘O’ in our my_str and the output would be:

Count of o in I Love Python is : 2

 

13 9 6
94,260 points

Related questions

0 votes
1 answer 40 views
40 views
Problem: I was trying to remove all occurrences of a character in a string python but I failed. The code I wrote is: def DeletChar(e, b) : counts = e.count(b) e = list(e) while counts : e.remove(b) counts -= 1 print(e) if __name__ == '__main__' : e = "geeksforgeeks" DeletChar(e,'g') The output ... e', 'k', 's', 'f', 'o', 'r', 'e', 'e', 'k', 's'] how to solve this problem I am a newbee please help
asked Mar 22, 2020 LizzyM 6.1k points
0 votes
1 answer 9 views
9 views
Problem: how to I Solve this: error: count number of characters in string python HELP! I need help understanding this: count number of characters in string python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 11 Mashhoodch 13k points
0 votes
1 answer 13 views
13 views
Problem: Please let me know any solution regarding this ..check length of string c++
asked Apr 20 Ifra 37.2k points
0 votes
1 answer 3 views
3 views
Problem: Help me to write a java program to count the number of words in a string.
asked 5 days ago Irenka Kiri 17.9k points
0 votes
1 answer 5 views
5 views
Problem: Linux count occurrences of string in file.
asked Apr 9 charles mathews 5.5k points
0 votes
1 answer 6 views
6 views
Problem: Hello ... Can anyone tell me that why I got this error? program to count number of characters in a string in c
asked Mar 25 Ifra 37.2k points
0 votes
1 answer 5 views
5 views
Problem: Hello! How can I count the number of characters in a string automatically through c#?
asked Oct 11, 2020 Code Learner 9.9k points
0 votes
1 answer 6 views
6 views
Problem: I have a vector of numbers. How can I have R count the number of times a value x appears in the vector?
asked Mar 30 ummesalma 29.2k points
0 votes
1 answer 10 views
10 views
Problem: How can I count number of occurrences in array in Javascript?
asked Mar 20 Ifra 37.2k points
0 votes
1 answer 3 views
3 views
problem: how i solve this errors of sql select distinct values and count of each .
asked Apr 16 ashik 14.6k points