#๐Ÿ”’ Why is string not a sting?

14 messages ยท Page 1 of 1 (latest)

mint hare
#

I just need help understanding why this is happening ^^

for this code where I'm just checking the class:

date = "1 11 2111"

def magic(string):
    m,d,y = string.split()
    return type(y)

print(magic(date))```
the answer come out as:
`<class 'str'>`

which makes sense because inside my function m,d,y do act like strings

but when I use endswith
```py
date = "1 11 2111"

def magic(string):
    m,d,y = string.split()
    return y.endswith(int(m)*int(d))

print(magic(date))```
I get an error:
`TypeError: endswith first arg must be str or a tuple of str, not int`

so for it to work I end up specifying that y is a string when it already is one ๐Ÿ˜– 
why is this happening?
inland roverBOT
#

@mint hare

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hard vault
#

You're casting both m and d to integers and multiplying them

#

then passing that result to endswith

#

return y.endswith(int(m)*int(d))

#

that result is also an integer

#

So you're passing an int.

#

Do you understand?

#

@mint hare

mint hare
hard vault
#

You're very welcome.

#

You can close this thread with !close

inland roverBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.