• Register
2 votes


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


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,240 points

Related questions

0 votes
1 answer 34 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 5 views
Problem: Hello! How can I count the number of characters in a string automatically through c#?
asked Oct 11, 2020 Code Learner 5.7k points
0 votes
1 answer 34 views
How to get the number from the occurences of individual characters Supose the string is: x = 'cdfghcfonccn' How can i solve this problem.
asked Mar 10, 2020 LizzyM 6.1k points
0 votes
1 answer 51 views
Say we've used pandas dataframe[column].value_counts() that outputs: apple 5 sausage 2 banana 2 cheese 1 How does one extract the values within the order same as shown higher than from min to max ?
asked Oct 29, 2020 psandprop 2.4k points
0 votes
1 answer 12 views
Problem: The question is to traverse given string, as soon as we find an occurrence of we keep traversing until we find a character which is not c. We also set a flag to indicate that one more occurrences of c are seen. If we see c again and flag is set, then we return false.
asked Nov 2, 2020 Mashhoodch 9.1k points
1 vote
1 answer 23 views
Problem: I tried to count occurrences of individual character of string using python. So I wrote this program for the string: a = 'yanwawny' The program: ​​​string = 'yanwawny' ​dict((letter, string.count(letter)) for letter in set(a) But it throws the error message: File "main.py", line 1 ​a = 'yanwawny' ^ SyntaxError: invalid character in identifier What to do to solve this thing?
asked May 4, 2020 samhaz 5k points
2 votes
1 answer 29 views
Problem: Hello guys, I hope you all are doing well. I am learning java programming in my school. My teacher taught us many things about the string in the last class and gave a problem to solve. I tried to solve the problem but couldn’t make it happen so far. ... problem I’d be grateful to him/her. So, my problem is: find all occurrences of a substring in a string java Thanks, mate.
asked Mar 25, 2020 Gavin 15.3k points
0 votes
1 answer 16 views
Problem: I am trying to count the occurrences of each letter of a word word = input("Enter a word") Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',' ... ): print(word.count(Alphabet[i])) This currently outputs the number of times each letter occurs including the ones that don't. How do I list the letters vertically with the frequency alongside
asked Jan 22 Mashhoodch 9.1k points
0 votes
1 answer 14 views
I am new. Please help me. solve this problem.
asked Nov 17, 2020 TeamScript 13.5k points
0 votes
1 answer 1 view
1 view
Problem: So if my string is "the dude is a cool dude". I'd like to find the first index of 'dude': mystring.findfirstindex('dude') # should return 4 What is the python command for this?
asked 1 day ago Mashhoodch 9.1k points