Hướng dẫn can you convert a string to a float in python - bạn có thể chuyển đổi một chuỗi thành một float trong python không

Python xác định các chức năng chuyển đổi loại để chuyển đổi trực tiếp một loại dữ liệu sang loại dữ liệu khác. Bài viết này nhằm mục đích cung cấp thông tin về việc chuyển đổi chuỗi thành float. Trong Python, chúng ta có thể sử dụng float () để chuyển đổi chuỗi thành float. và chúng ta có thể sử dụng int () để chuyển đổi chuỗi thành số nguyên.float() to convert String to float. and we can use int() to convert String to an integer.

Chương trình Python để phân tích một chuỗi vào một chiếc phao

Hàm này được sử dụng để chuyển đổi bất kỳ loại dữ liệu nào thành số điểm nổi.

Cú pháp của phao:

float(x)

Phương thức chỉ chấp nhận một tham số và đó cũng là tùy chọn để sử dụng. Nếu không có đối số được truyền thì phương thức trả về 0,0.

Ví dụ 1: phân tích một chuỗi vào một chiếc phao

Trong ví dụ này, chúng tôi đang chuyển đổi chuỗi thành float.

Python3

3.141
<type 'str'>
3.141
<type 'float'>
7
3.141
<type 'str'>
3.141
<type 'float'>
8
3.141
<type 'str'>
3.141
<type 'float'>
9

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
1

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
4
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
5

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6
3.141
<type 'str'>
3.141
<type 'float'>
8
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
8
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
9

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6
int()
3

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
4
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6
int()
9

Output:

3.141
<type 'str'>
3.141
<type 'float'>

Ví dụ 2: Một chuỗi float số vào phao

Trong ví dụ này, chúng tôi cố gắng chuyển đổi một chuỗi thành một số nguyên và sau đó là một float. Trong đầu ra, chúng ta có thể thấy rằng chúng ta có thể convent một số float chuỗi vào một số nguyên, do đó, chúng ta sẽ gặp lỗi giá trị.

Python3

<class 'int'>
55567
0
3.141
<type 'str'>
3.141
<type 'float'>
8
<class 'int'>
55567
2

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6
3.141
<type 'str'>
3.141
<type 'float'>
8
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
8
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
<class 'int'>
55567
7
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
5

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
4
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6
int()
9

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
<class 'int'>
15
7
<class 'int'>
15
8
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6
int()
3

Output:

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'

Chương trình Python để phân tích một chuỗi vào int

Hàm này được sử dụng để chuyển đổi bất kỳ loại dữ liệu nào thành số nguyên.

Cú pháp của int

int()

Ví dụ 1: phân tích một chuỗi thành một số nguyên

Trong ví dụ này, chúng tôi đang chuyển đổi chuỗi thành số nguyên.

Python3

<class 'int'>
55567
0
3.141
<type 'str'>
3.141
<type 'float'>
8
>>> float("545.2222")
545.2222
3

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6
3.141
<type 'str'>
3.141
<type 'float'>
8
<class 'int'>
55567
7
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
1

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
4
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6
int()
9

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
<class 'int'>
15
7
<class 'int'>
15
8
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
6__

Output:

<class 'int'>
55567

Ví dụ 2: Một chuỗi nổi số vào một số nguyên

Trong ví dụ này, chúng tôi cố gắng chuyển đổi chuỗi float thành float và sau đó thành số nguyên.

Python3

>>> int("0b11111", 2)
31
>>> int("11111", 2)
31
>>> int('0o37', 8)
31
>>> int('37', 8)
31
>>> int('0x1f', 16)
31
>>> int('1f', 16)
31
0
3.141
<type 'str'>
3.141
<type 'float'>
8
>>> int("0b11111", 2)
31
>>> int("11111", 2)
31
>>> int('0o37', 8)
31
>>> int('37', 8)
31
>>> int('0x1f', 16)
31
>>> int('1f', 16)
31
2

>>> int("0b11111", 2)
31
>>> int("11111", 2)
31
>>> int('0o37', 8)
31
>>> int('37', 8)
31
>>> int('0x1f', 16)
31
>>> int('1f', 16)
31
3
3.141
<type 'str'>
3.141
<type 'float'>
8
<class 'int'>
55567
7
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
8
>>> int("0b11111", 2)
31
>>> int("11111", 2)
31
>>> int('0o37', 8)
31
>>> int('37', 8)
31
>>> int('0x1f', 16)
31
>>> int('1f', 16)
31
8

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
3
ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
4
>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
2

ValueError                                Traceback (most recent call last)
<ipython-input-4-8771a1bc4294> in <module>()
      1 string = '55.567'
      2 
----> 3 Float = float(int(string))
      4 
      5 print(type(Float))

ValueError: invalid literal for int() with base 10: '55.567'
0
>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
4

Output:

<class 'int'>
15

Trong Python, làm thế nào tôi có thể phân tích một chuỗi số như "545.2222" với giá trị float tương ứng của nó, 542.2222? Hoặc phân tích chuỗi "31" cho một số nguyên, 31? Tôi chỉ muốn biết làm thế nào để phân tích một chuỗi float vào một float và (riêng biệt) một chuỗi int vào int. I just want to know how to parse a float string to a float, and (separately) an int string to an int.

Thật tốt khi bạn yêu cầu làm những điều này một cách riêng biệt. Nếu bạn đang trộn chúng, bạn có thể sẽ tự đặt ra các vấn đề sau này. Câu trả lời đơn giản là:

>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
5 để nổi:

>>> float("545.2222")
545.2222

>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
6 cho một số nguyên:

>>> int("31")
31

Các chuyển đổi khác, ints đến và từ các chuỗi và chữ:

Chuyển đổi từ các cơ sở khác nhau và bạn nên biết cơ sở trước (10 là mặc định). Lưu ý Bạn có thể đặt tiền tố chúng với những gì Python mong đợi cho các chữ của nó (xem bên dưới) hoặc xóa tiền tố:

>>> int("0b11111", 2)
31
>>> int("11111", 2)
31
>>> int('0o37', 8)
31
>>> int('37', 8)
31
>>> int('0x1f', 16)
31
>>> int('1f', 16)
31

Nếu bạn không biết trước cơ sở, nhưng bạn biết họ sẽ có tiền tố chính xác, Python có thể suy ra điều này cho bạn nếu bạn vượt qua

>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
7 làm cơ sở:

>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31

Không phân giải (tức là số nguyên) theo các cơ sở khác

Tuy nhiên, nếu động lực của bạn là có mã của riêng bạn thể hiện rõ ràng các giá trị cụ thể được mã hóa cứng, bạn có thể không cần chuyển đổi từ các cơ sở - bạn có thể để Python tự động làm điều đó với cú pháp chính xác.

Bạn có thể sử dụng tiền tố Apropos để chuyển đổi tự động cho các số nguyên với các chữ cái sau. Chúng có giá trị cho Python 2 và 3:

Nhị phân, tiền tố

>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
8

3.141
<type 'str'>
3.141
<type 'float'>
0

Octal, tiền tố

>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
9

3.141
<type 'str'>
3.141
<type 'float'>
1

Thập lục phân, tiền tố

3.141
<type 'str'>
3.141
<type 'float'>
00

3.141
<type 'str'>
3.141
<type 'float'>
2

Điều này có thể hữu ích khi mô tả các cờ nhị phân, quyền tệp trong mã hoặc giá trị hex cho màu - ví dụ: lưu ý không có báo giá:

3.141
<type 'str'>
3.141
<type 'float'>
3

Làm cho Python 2 Octals mơ hồ tương thích với Python 3

Nếu bạn thấy một số nguyên bắt đầu bằng 0, trong Python 2, thì đây là cú pháp octal (không dùng nữa).

3.141
<type 'str'>
3.141
<type 'float'>
4

Nó là xấu bởi vì nó trông giống như giá trị phải là

3.141
<type 'str'>
3.141
<type 'float'>
01. Vì vậy, trong Python 3, bây giờ nó làm tăng một
3.141
<type 'str'>
3.141
<type 'float'>
02:

3.141
<type 'str'>
3.141
<type 'float'>
5

Chuyển đổi Python 2 của bạn thành Octals hoạt động trong cả 2 và 3 với tiền tố

>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
9:

3.141
<type 'str'>
3.141
<type 'float'>
1

Chuỗi có thể được chuyển đổi thành float không?

Chúng ta có thể chuyển đổi chuỗi thành float trong java bằng phương thức float.parseFloat (). parseFloat() method.

Tại sao Python không thể chuyển đổi chuỗi thành float?

Python "valueError: Không thể chuyển đổi chuỗi thành float" xảy ra khi chúng ta chuyển một chuỗi không thể chuyển đổi thành một float (ví dụ: một chuỗi trống hoặc một chuỗi chứa các ký tự) sang lớp float ().Để giải quyết lỗi, xóa tất cả các ký tự không cần thiết khỏi chuỗi.occurs when we pass a string that cannot be converted to a float (e.g. an empty string or one containing characters) to the float() class. To solve the error, remove all unnecessary characters from the string.

Làm cách nào để chuyển đổi một chuỗi thành danh sách float?

Cách pythonic nhất để chuyển đổi danh sách các chuỗi thành danh sách phao là sử dụng danh sách hiểu biết float = [float (x) cho x trong chuỗi].Nó lặp lại trên tất cả các phần tử trong danh sách và chuyển đổi từng phần tử danh sách X thành giá trị float bằng cách sử dụng hàm tích hợp float (x).use the list comprehension floats = [float(x) for x in strings] . It iterates over all elements in the list and converts each list element x to a float value using the float(x) built-in function.

Làm thế nào để bạn chuyển đổi một chuỗi thành một số nguyên hoặc một chiếc phao trong Python?

Trong Python, bạn có thể chuyển đổi một chuỗi str thành int số nguyên và số điểm nổi nổi với int () và float ().Bài viết này mô tả các nội dung sau đây.Sử dụng str () để chuyển đổi số số nguyên hoặc dấu nổi thành một chuỗi.Bạn cũng có thể chuyển đổi một danh sách các chuỗi thành một danh sách các số.int() and float() . This article describes the following contents. Use str() to convert an integer or floating point number to a string. You can also convert a list of strings to a list of numbers.