+ 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.
4 Antworten
+ 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 😀
+ 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
+ 1
Thanks
+ 1
Thank you all..finally my code worked.