Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. int() / float() / str() / bool()

int() / float() / str() / bool()

These are the basic built-in functions for converting a value to a different type. They are commonly used when processing user input or preparing values before performing calculations.

Syntax

# Converts a value to an integer. You can optionally specify the base (radix).
int(value, base=10)

# Converts a value to a floating-point number.
float(value)

# Converts a value to a string.
str(value)

# Converts a value to a boolean (True or False).
bool(value)

# Converts a value to a complex number.
complex(real, imag=0)

# Converts a value to a bytes object.
bytes(value, encoding)

Function List

FunctionDescription
int(x, base=10)Converts an integer, float, or string to an integer. When converting a string, you can specify the base (2–36).
float(x)Converts an integer or string to a floating-point number. Strings like 'inf' and 'nan' are also accepted.
str(obj)Converts any object to a human-readable string representation.
bool(x)Converts a value to True or False. Values such as 0, an empty string, None, and an empty list evaluate to False.
complex(real, imag=0)Creates a complex number from a real part and an imaginary part. A string can also be used as input.
bytes(source, encoding)Converts a string to a bytes object. When the source is a string, an encoding (e.g., 'utf-8') must be specified.

Sample Code

# Use int() to convert various values to integers.
print(int(3.9))      # 3 (decimal part is truncated)
print(int("42"))     # 42 (string to integer)
print(int("FF", 16)) # 255 (hexadecimal string to integer)
print(int("1010", 2)) # 10 (binary string to integer)
print(int(True))     # 1 (True is converted to 1)

# Use float() to convert values to floating-point numbers.
print(float(10))       # 10.0
print(float("3.14"))   # 3.14
print(float("inf"))    # inf (infinity)
print(float("nan"))    # nan (not a number)

# Use str() to convert values to strings.
print(str(100))         # '100'
print(str(3.14))        # '3.14'
print(str(True))        # 'True'
print(str([1, 2, 3]))   # '[1, 2, 3]'

# Use bool() to see which values evaluate to False.
print(bool(0))     # False
print(bool(""))    # False (empty string)
print(bool([]))    # False (empty list)
print(bool(None))  # False
print(bool(1))     # True
print(bool("a"))   # True
print(bool([0]))   # True (non-empty list containing 0)

# Use complex() to create a complex number.
c = complex(3, 4)
print(c)         # (3+4j)
print(c.real)    # 3.0 (real part)
print(c.imag)    # 4.0 (imaginary part)

# Use bytes() to convert a string to a bytes object.
b = bytes("Hello", "utf-8")
print(b)          # The UTF-8 byte sequence is printed.
print(len(b))     # 5 (one byte per character for ASCII)

Overview

Python is a dynamically typed language, but type conversions must be performed explicitly. In particular, values received from input() are always strings, so you need to convert them with int() or float() before using them in calculations.

When you convert a floating-point number with int(), the decimal part is truncated (rounded toward zero). If you need to round to the nearest integer, use the built-in round() function instead.

The conversion rules for bool() — known as falsy and truthy values — are an important concept in Python's conditional expressions. Values that evaluate to False include 0, 0.0, "" (empty string), [] (empty list), {} (empty dict), () (empty tuple), set() (empty set), and None. Any value not in this list evaluates to True. For information on checking types, see len() / type() / id().

If you find any errors or copyright issues, please .