• Register
1 vote
21 views

Problem:

I was tring to find the length of a string in python. So I wrote a code:

string = 0
for c in "popular": 
    string+==1 
print (string) 

which gives following error;

File "ab.py", line 3
    string+==1 
             ^
SyntaxError: invalid syntax

Is this the right way to do this or there are other waysto find this out?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

To find out the length of a string in python you should have wrote your code as:
string = 0
for c in "popular": 
    string+=1 
print (string) 

or you also can use len() method for this :

string = "popular" 
print(len(string))

And the both output will be

7

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 24 views
24 views
I am new to python programming I was trying to check if a letter is uppercase in python. I wrote some code to check this statement : import string def UpperCase(s_str): for x in s_str: if x not in string.UpeerCase: return False return True But I can not get the actual ... last): File "<stdin>", line 1, in <module> NameError: name 'LoWWE' is not defined Can anyone help me to solve this thing?
asked Mar 14, 2020 LizzyM 6.1k points
1 vote
1 answer 37 views
37 views
Problem: I am beginner in pyhton prgramming language, I tried to convert string array to int array using python so I wrote a code which contains- testing_list = ['4', '2', '3', '9', '5'] And my code is: testing_list = ['4', '2', '3', '9', '5 ... list is : str(testing_list) Converted list is : str(testing_list) why am I not getting the converted result and what is the way to get the acurate result?
asked Mar 29, 2020 LizzyM 6.1k points
1 vote
1 answer 83 views
83 views
Problem: I was trying to remove a letter from a string by writing this python program: def removechar(p, q) : counts = p.count(q) p = list(p) while counts : p.remove(q) counts -= 1 print(p) if __name__ == '__main__' : p = "nooneisallinall" removechar(p,'n') And the program ... 'l', 'l', 'i', 'a', 'l', 'l'] How can I get the output as string after removing the specific letter. Thanks in advance.
asked May 16, 2020 samhaz 5k points
1 vote
1 answer 41 views
41 views
Problem: Hello I am starting my programming experience with python recently. I&rsquo;ve been surfing over the internet for a program that can remove punctuation from a String. I&rsquo;ve found many of them but still can&rsquo;t understand them. I am looking for a program written in very basic codes. If possible, please write me a program which would be easy to understand for me. Thanks
asked Mar 8, 2020 Gavin 15.3k points
1 vote
1 answer 23 views
23 views
Problem: I am beginner in pyhton programing. I have a dictionary of string and int and that is: Dictionary = { "Hola": 65, "tart" : 32, "star" : 34, "site" : 55, "pearl" : 88 } Supose I have to remove a key from this dictionary what should I do next.
asked Mar 28, 2020 LizzyM 6.1k points
1 vote
1 answer 587 views
587 views
Problem: I am getting the following error : error: array initializer must be an initializer list or string literal For the array; char x[80] = GetString(); Can any body explain or help?
asked Apr 2, 2020 LizzyM 6.1k points
0 votes
1 answer 19 views
19 views
I am new to python programming.. I was trying to check if a string is empty or whitespace in a code : Mystr= "" Yourstr = " " print ("This string is empty : ", end = "") if(not Yourstr): print ("Yes") print ("This string is empty : ", end = "") if(not Mystr): print ("No") It&rsquo;s output is : This string is empty : This string is empty : No Its not giving me actual result what can I do now?
asked Mar 16, 2020 LizzyM 6.1k points
2 votes
1 answer 23 views
23 views
Problem: Hello all, I am a new student in the Python programming language. I am learning through a textbook. I just completed the String chapter and started solving the problems of this chapter. Some of the problems are easy to solve for me. I stuck on a point ... understand. I&rsquo;ve seen some of the solutions answered by the good people. If anybody could help me would be great for me. Thanks
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 249 views
249 views
As I analyzed it I have arrived on a conclusion but I don't think that its gonna work so I would appreciate if you correct it. If I want to enter a number again to be added on the sum variable what can be the decision ? Start Initialize counter = 0 , sum = 0 Input N N % 3 == ... else if counter != N { If true , loop back to " Input N " else print sum } else , N not divisible by 3 . Print sum End
asked Oct 25, 2020 psandprop 2.4k points