Length in python | SoloLearn: Learn to code for FREE!


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.

12/9/2020 9:45:39 AM

Harshita Sharma

5 Answers

New Answer


s = "this is a short string" print(f'String has {len(s)} characters at all.') print(f'Without character "t" there are {len(s)-s.count("t")} characters, including spaces.')


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 😀


def getlen(s, c): return len(s) - s.count(c) print(getlen("string", 'r')) edit...see here for string methods.. https://www.w3schools.com/python/python_ref_string.asp



Thank you all..finally my code worked.