Hướng dẫn can you concatenate strings and floats in python? - bạn có thể nối các chuỗi và thả nổi trong python không?

32

Show

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Giáo viên hình học của chúng tôi đã cho chúng tôi một nhiệm vụ yêu cầu chúng tôi tạo ra một ví dụ về khi nào đồ chơi sử dụng hình học trong cuộc sống thực, vì vậy tôi nghĩ sẽ rất tuyệt khi tạo ra một chương trình tính toán có bao nhiêu gallon nước để lấp đầy một nhóm hình dạng, và với các kích thước nhất định.

Đây là chương trình cho đến nay:

import easygui
easygui.msgbox("This program will help determine how many gallons will be needed to fill up a pool based off of the dimensions given.")
pool=easygui.buttonbox("What is the shape of the pool?",
              choices=['square/rectangle','circle'])
if pool=='circle':
height=easygui.enterbox("How deep is the pool?")
radius=easygui.enterbox("What is the distance between the edge of the pool and the center of the pool (radius)?")
easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")

Tôi vẫn nhận được lỗi này mặc dù:

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects

tôi làm gì?

Hướng dẫn can you concatenate strings and floats in python? - bạn có thể nối các chuỗi và thả nổi trong python không?

Nircraft

7.7165 huy hiệu vàng29 Huy hiệu bạc45 Huy hiệu đồng5 gold badges29 silver badges45 bronze badges

Đã hỏi ngày 5 tháng 6 năm 2013 lúc 19:31Jun 5, 2013 at 19:31

0

Tất cả các loại dữ liệu phao hoặc không chuỗi phải được đúc vào chuỗi trước khi kết nối

Điều này sẽ hoạt động chính xác: (Lưu ý diễn viên

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
2 cho kết quả nhân)

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")

Trực tiếp từ thông dịch viên:

>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.

Đã trả lời ngày 28 tháng 6 năm 2013 lúc 13:23Jun 28, 2013 at 13:23

Kalyan02Kalyan02Kalyan02

1.38610 Huy hiệu bạc16 Huy hiệu đồng10 silver badges16 bronze badges

1

Có một giải pháp nữa, bạn có thể sử dụng định dạng chuỗi (tương tự như ngôn ngữ C tôi đoán)

Bằng cách này, bạn cũng có thể kiểm soát độ chính xác.

radius = 24
height = 15

msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

không có độ chính xác

Bạn cần 27129.600000 gallon nước để lấp đầy hồ bơi này.

Với độ chính xác 8.2

Bạn cần 27129,60 gallon nước để lấp đầy hồ bơi này.

Đã trả lời ngày 17 tháng 5 năm 2019 lúc 19:00May 17, 2019 at 19:00

Gaurang Shahgaurang ShahGaurang Shah

10,7K5 Huy hiệu vàng67 Huy hiệu bạc121 Huy hiệu Đồng5 gold badges67 silver badges121 bronze badges

Với Python3.6+, bạn có thể sử dụng F-Strings để định dạng các câu lệnh in.

radius=24.0
height=15.0
print(f"You need {3.14*height*radius**2:8.2f} gallons of water to fill this pool.")

Đã trả lời ngày 3 tháng 10 năm 2019 lúc 0:50Oct 3, 2019 at 0:50

TyberiustyberiusTyberius

6172 Huy hiệu vàng11 Huy hiệu bạc19 Huy hiệu đồng2 gold badges11 silver badges19 bronze badges

Bài viết này mô tả cách kết hợp các chuỗi trong Python.

  • Concatenate nhiều chuỗi:
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    3, nhà điều hành
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    4
  • Các chuỗi và số Concatenate:
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    3, toán tử
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    4,
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    7,
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    8, F-String
  • Concatenate một danh sách các chuỗi thành một chuỗi:
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    9
  • Concatenat một danh sách các số vào một chuỗi:
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    9,
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    7

Concatenate nhiều chuỗi: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 3, nhà điều hành easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 4

Các chuỗi và số Concatenate: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 3, toán tử easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 4, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 7, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 8, F-String

Nhà điều hành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3

s = 'aaa' + 'bbb' + 'ccc'
print(s)
# aaabbbccc

s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'

s = s1 + s2 + s3
print(s)
# aaabbbccc

s = s1 + s2 + s3 + 'ddd'
print(s)
# aaabbbcccddd

Bạn có thể kết hợp các chuỗi chữ (>>> radius = 10 >>> height = 10 >>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") >>> print msg You need 3140.0gallons of water to fill this pool. 5 hoặc >>> radius = 10 >>> height = 10 >>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") >>> print msg You need 3140.0gallons of water to fill this pool. 6) và các biến chuỗi với toán tử easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 3.

Nhà điều hành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4

s1 += s2
print(s1)
# aaabbb

Bạn có thể nối một chuỗi khác vào một chuỗi với toán tử tại chỗ,

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4. Chuỗi bên phải được nối sau khi biến chuỗi ở bên trái.

s = 'aaa'

s += 'xxx'
print(s)
# aaaxxx

Nếu bạn muốn thêm một chuỗi vào cuối biến chuỗi, hãy sử dụng toán tử easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 4.

Concatenate bằng cách viết chuỗi chữ liên tiếp

s = 'aaa''bbb''ccc'
print(s)
# aaabbbccc

Nếu bạn viết chuỗi chữ liên tiếp, chúng được nối.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
0

Sử dụng điều này, bạn có thể viết các chuỗi dài trên nhiều dòng trong mã.

  • Viết một chuỗi dài trên nhiều dòng trong Python

Bạn không thể làm điều này cho các biến chuỗi.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
1

Các chuỗi và số Concatenate: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 3, toán tử easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 4, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 7, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 8, F-String

Hoạt động

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3 giữa các loại khác nhau làm tăng lỗi.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
2

Nếu bạn muốn kết hợp một chuỗi và một số, chẳng hạn như số nguyên

radius = 24
height = 15

msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)
7 hoặc điểm nổi
radius = 24
height = 15

msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)
8, hãy chuyển đổi số thành một chuỗi bằng
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
7 và sau đó sử dụng toán tử
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3 hoặc toán tử
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
3

Sử dụng hàm

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
8 hoặc phương thức chuỗi
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
8 nếu bạn muốn chuyển đổi định dạng số, chẳng hạn như đệm không hoặc số thập phân.

  • Chuỗi - Đặc điểm kỹ thuật của ngôn ngữ Mini - Python 3.8.1 Tài liệu

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
4

Tất nhiên, cũng có thể nhúng giá trị của một biến trực tiếp trong một chuỗi mà không chỉ định định dạng, đơn giản hơn so với sử dụng toán tử

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
5

Trong Python 3.6 trở lên, bạn cũng có thể sử dụng một chuỗi theo nghĩa đen (chuỗi F) được định dạng. Nó thậm chí còn đơn giản hơn

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
8.

  • 2. Phân tích từ vựng - Chuỗi được định dạng theo nghĩa đen - Python 3.9.4 Tài liệu

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
6

Concatenate một danh sách các chuỗi thành một chuỗi: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 9

Bạn có thể kết hợp một danh sách các chuỗi vào một chuỗi với phương thức chuỗi,

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9.

  • Các loại tích hợp - str - tham gia () - Python 3.8.1 Tài liệu

Gọi phương thức

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9 từ
radius=24.0
height=15.0
print(f"You need {3.14*height*radius**2:8.2f} gallons of water to fill this pool.")
9 và vượt qua
s = 'aaa' + 'bbb' + 'ccc'
print(s)
# aaabbbccc

s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'

s = s1 + s2 + s3
print(s)
# aaabbbccc

s = s1 + s2 + s3 + 'ddd'
print(s)
# aaabbbcccddd
0.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
7

Nếu bạn sử dụng một chuỗi trống

s = 'aaa' + 'bbb' + 'ccc'
print(s)
# aaabbbccc

s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'

s = s1 + s2 + s3
print(s)
# aaabbbccc

s = s1 + s2 + s3 + 'ddd'
print(s)
# aaabbbcccddd
1,
s = 'aaa' + 'bbb' + 'ccc'
print(s)
# aaabbbccc

s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'

s = s1 + s2 + s3
print(s)
# aaabbbccc

s = s1 + s2 + s3 + 'ddd'
print(s)
# aaabbbcccddd
0 chỉ đơn giản là được nối và nếu bạn sử dụng dấu phẩy
s = 'aaa' + 'bbb' + 'ccc'
print(s)
# aaabbbccc

s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'

s = s1 + s2 + s3
print(s)
# aaabbbccc

s = s1 + s2 + s3 + 'ddd'
print(s)
# aaabbbcccddd
3, thì nó sẽ tạo ra một chuỗi phân loại bằng dấu phẩy. Nếu một ký tự mới
s = 'aaa' + 'bbb' + 'ccc'
print(s)
# aaabbbccc

s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'

s = s1 + s2 + s3
print(s)
# aaabbbccc

s = s1 + s2 + s3 + 'ddd'
print(s)
# aaabbbcccddd
4 được sử dụng, một dòng mới sẽ được chèn cho mỗi chuỗi.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
8

Lưu ý rằng các đối tượng có thể đi được khác như Tuples có thể được chỉ định là đối số của

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9.

Sử dụng

s = 'aaa' + 'bbb' + 'ccc'
print(s)
# aaabbbccc

s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'

s = s1 + s2 + s3
print(s)
# aaabbbccc

s = s1 + s2 + s3 + 'ddd'
print(s)
# aaabbbcccddd
6 để phân chia một chuỗi được phân tách bằng một dấu phân cách cụ thể và lấy nó làm danh sách. Xem bài viết sau đây để biết chi tiết.

  • Chia chuỗi trong Python (Delimiter, Line Break, Regex, v.v.)

Concatenat một danh sách các số vào một chuỗi: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 9, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 7

Nếu bạn đặt một danh sách không chuỗi thành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9, một lỗi sẽ được nêu ra.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
9

Nếu bạn muốn kết hợp một danh sách các số (

radius = 24
height = 15

msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)
7 hoặc
radius = 24
height = 15

msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)
8) vào một chuỗi, hãy áp dụng hàm
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
7 cho mỗi phần tử trong danh sách hiểu để chuyển đổi số thành chuỗi, sau đó kết nối chúng với
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9.

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
0

Nó có thể được viết dưới dạng biểu thức máy phát, một phiên bản trình tạo của toàn bộ danh sách. Các biểu thức của máy phát được đặt trong ngoặc đơn

s1 += s2
print(s1)
# aaabbb
4, nhưng bạn có thể bỏ qua
s1 += s2
print(s1)
# aaabbb
4 nếu biểu thức trình tạo là đối số duy nhất của hàm hoặc phương thức.

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
1

Nói chung, các biểu thức của máy phát có lợi thế là giảm sử dụng bộ nhớ so với sự hiểu biết danh sách. Tuy nhiên, vì

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9 nội bộ chuyển đổi một trình tạo thành một danh sách, không có lợi thế nào để sử dụng các biểu thức tạo.

  • Python - Danh sách so với tốc độ hiểu biết của máy phát với chức năng tham gia - Stack Overflow

Xem bài viết sau đây để biết chi tiết về sự hiểu biết danh sách và biểu thức máy phát.

  • Liệt kê sự hiểu biết trong Python

Bạn có thể nối các chuỗi và biến trong Python không?

Làm thế nào để kết hợp các chuỗi trong Python. Trong mã ở trên, chúng tôi đã tạo hai biến (x và y) cả hai chuỗi - "hạnh phúc" và "mã hóa" - và biến thứ ba (z) kết hợp hai biến chúng tôi tạo ban đầu. Chúng tôi đã có thể kết hợp hai biến bằng cách sử dụng toán tử +.We were able to combine the two variables by using the + operator.

Làm thế nào để bạn kết hợp các chuỗi và đối tượng trong Python?

Kết nối chuỗi bằng toán tử + dễ sử dụng + toán tử để nối chuỗi.Tuy nhiên, các đối số phải là một chuỗi.Chúng ta có thể sử dụng hàm str () để có được biểu diễn chuỗi của một đối tượng.use str() function to get the string representation of an object.

Bạn có thể kết hợp các loại khác nhau trong Python không?

Rất dễ sử dụng toán tử + để nối chuỗi.Toán tử này có thể được sử dụng để thêm nhiều chuỗi lại với nhau.

Chúng ta có thể nối chuỗi và số trong Python không?

Python hỗ trợ nối chuỗi bằng toán tử +.Trong hầu hết các ngôn ngữ lập trình khác, nếu chúng ta nối một chuỗi với số nguyên (hoặc bất kỳ loại dữ liệu nguyên thủy nào khác), ngôn ngữ sẽ chăm sóc chúng thành một chuỗi và sau đó kết hợp nó.. In most other programming languages, if we concatenate a string with an integer (or any other primitive data types), the language takes care of converting them to a string and then concatenates it.