Length in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Length in python

How can we write length of a string without including a particular character For e.g length of "string" could be calculated as 5 by not including r.

9th Dec 2020, 9:45 AM
Harshita Sharma
Harshita Sharma - avatar
4 Answers
+ 4
Do it this way: - Create a variable to add the length to - Loop through the string - Use a conditional statement (if an element is not something) - Add it to the variable. - Print the variable Snippet: length = 0 for letter in string: if letter != the letter you don't want: length += 1 print (length) Happy Coding 😀
9th Dec 2020, 9:50 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 4
def getlen(s, c): return len(s) - s.count(c) print(getlen("string", 'r')) edit...see here for string methods.. https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_ref_string.asp
9th Dec 2020, 9:59 AM
rodwynnejones
rodwynnejones - avatar
+ 1
Thanks
9th Dec 2020, 9:52 AM
Harshita Sharma
Harshita Sharma - avatar
+ 1
Thank you all..finally my code worked.
9th Dec 2020, 5:10 PM
Harshita Sharma
Harshita Sharma - avatar