Python String to Integer | David Capella
View on GitHub

David Capella

Data Engineer

Python String to Integer

HackerRank

Read a string, S, and print its integer value; if S cannot be converted to an integer, print Bad String.

It’s pretty straight forward.

Code

Try print(int(s))
try:
  print(int(S))
Except print(“Bad String”)
try:
  print(int(S))
except:
  print("Bad String")

Final Code

#!/bin/python3

import sys


S = input().strip()

try:
  print(int(S))
except:
  print("Bad String")

Conclusion

It’s always good to practice a try-except.