Question:
Try to find out how many zeros a given number has at the end.
Input: A positive Int
Output: An Int.
def end_zeros(num: int) -> int:
# your code here
return
if __name__ == '__main__':
print("Example:")
print(end_zeros(0))
Solution:
uese rstrip() method
example1
Remove white space at the end of string
txt = " banana "
x = txt.rstrip()
print("of all fruits", x, "is my favorite")
the result is :
of all fruits banana is my favorite
example 2
remove trailing characters if they are commas, s,q or w
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)
the result is :
banana
**
Define and Usage
The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.
syntax
string.rstrip(characters)
parameter values
Parameter | Description |
---|---|
Characters | Optional. A set of characters to remove as trailing characters |
Answer:
def end_zeros(num: int) -> int:
# your code here
s=str(num)
return len(s)-len(s.rstrip('0'))
if __name__ == '__main__':
print("Example:")
print(end_zeros(0))
- [attention!]:
0 is not a number anymore. Instead, it is a string right now.
The mistake I confront was that I didnot put single quotes around it, which resulted in unmatch.