Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Convert String to Integer

Convert a numeric string into an integer, handling optional whitespace and sign characters.

Python practice15 minVariables & Data TypesIntermediateLast updated December 29, 2025

Problem statement

Create a function that accepts a string representing an integer and returns its integer value. The input string may include leading or trailing whitespace and an optional '+' or '-' sign. You may assume the string represents a valid integer (no letters or decimal points). Do not print; return the integer.

Task

Write a function that converts a string containing an integer into a Python int.

Examples

Simple positive number

Input

str_to_int('42')

Output

42

The string '42' converts to the integer 42.

Whitespace and negative number

Input

str_to_int(' -7 ')

Output

-7

Leading/trailing whitespace is ignored and the sign is preserved.

Input format

A single string argument is passed to str_to_int(s).

Output format

Return the integer value represented by the string.

Constraints

Input strings are guaranteed to represent valid integers. Use int() conversion and return the result.

Samples

Sample 1

Input

str_to_int('0')

Output

0

The string '0' converts to integer 0.