Integer Check for a String

Write a Python function that takes a string as input and checks if the string can be converted to an integer. The function should return True if the string can be converted to an integer, and False otherwise.

Example 1:

Input: "12345"
Output: True

Example 2:

Input: "1234abc"
Output: False

Consider using exception handling.

def check_integer(s):
try:
int(s)
return True
except ValueError:
return False

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!