Home >Backend Development >Python Tutorial >How to use the int() function in Python to convert a string or floating point number to an integer

How to use the int() function in Python to convert a string or floating point number to an integer

WBOY
WBOYOriginal
2023-08-22 16:39:273786browse

How to use the int() function in Python to convert a string or floating point number to an integer

How to use the int() function in Python to convert a string or floating point number into an integer

In Python, the int() function is used to convert a string or floating point number into an integer. Built-in function for converting floating point numbers to integers. This function is very convenient and can be used when you need to convert a string or floating point number to an integer. In this article, we will introduce the use of the int() function and show how to use it through code examples.

The basic usage of the int() function is very simple. It accepts a parameter, which can be an integer, a string or a floating point number. Here are some common usage examples:

  1. Convert a string to an integer:
num_str = "12345"
num_int = int(num_str)
print(num_int)  # 输出 12345
print(type(num_int))  # 输出 <class 'int'>,表示变量 num_int 的类型是整数

In the above code, we define a string num_str, whose The value is "12345". Convert this string to an integer using the int() function and assign the result to the variable num_int. Then we print out the value of num_int and we can see that its result is 12345 and its type is integer.

  1. Convert floating point number to integer:
num_float = 3.14
num_int = int(num_float)
print(num_int)  # 输出 3
print(type(num_int))  # 输出 <class 'int'>

In the above code, we define a floating point number num_float, whose value is 3.14. Convert this floating point number to an integer using the int() function and assign the result to the variable num_int. Then we print out the value of num_int and we can see that its result is 3 and its type is integer.

It should be noted that when using the int() function to convert a floating point number into an integer, the decimal part will be rounded off and only the integer part will be taken.

  1. When using the int() function to convert a string, you can also specify the base.
num_str = "FF"
num_int = int(num_str, 16)
print(num_int)  # 输出 255
print(type(num_int))  # 输出 <class 'int'>

In the above code, we define a string num_str whose value is "FF". Convert this string to an integer using the int() function and specifying 16 as the second argument (representing hexadecimal), and assign the result to the variable num_int. Then we print out the value of num_int and we can see that the result is 255 and the type is still an integer.

In addition to the above examples, you can also use the int() function to perform more complex conversion operations. For example, you can convert each element in a list or tuple to an integer.

nums_str = ["1", "2", "3", "4", "5"]
nums_int = [int(num) for num in nums_str]
print(nums_int)  # 输出 [1, 2, 3, 4, 5]

In the above code, we define a list nums_str, which contains some string elements. Using list comprehensions, convert each element to an integer using the int() function and store the result in a new list nums_int. Then we print out the value of nums_int and we can see that the result is [1, 2, 3, 4, 5].

To summarize, it is very simple to convert a string or floating point number to an integer using the int() function. Just call the int() function and pass in the object to be converted as a parameter. I hope the code examples in this article can help you better understand the specific usage of the int() function and use it flexibly in actual development.

The above is the detailed content of How to use the int() function in Python to convert a string or floating point number to an integer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn