Our geometry teacher gave us an assignment asking us to create an example of when toy use geometry in real life, so I thought it would be cool to make a program that calculates how many gallons of water will be needed to fill a pool of a certain shape, and with certain dimensions.
Here is the program so far:
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."]
i keep getting this error though:
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
what do i do?
nircraft
7,6665 gold badges29 silver badges45 bronze badges
asked Jun 5, 2013 at 19:31
0
All floats or non string data types must be casted to strings before concatenation
This should work correctly: [notice the str
cast for multiplication result]
easygui.msgbox=["You need "+ str[3.14*[float[radius]**2] * float[height]] + "gallons of water to fill this pool."]
straight from the interpreter:
>>> 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.
answered Jun 28, 2013 at 13:23
Kalyan02Kalyan02
1,38610 silver badges16 bronze badges
1
There is one more solution, You can use string formatting [similar to c language I guess]
This way you can control the precision as well.
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]
without precision
You need 27129.600000 gallons of water to fill this pool.
With precision 8.2
You need 27129.60 gallons of water to fill this pool.
answered May 17, 2019 at 19:00
Gaurang ShahGaurang Shah
10.6k5 gold badges65 silver badges119 bronze badges
With Python3.6+, you can use f-strings to format print statements.
radius=24.0
height=15.0
print[f"You need {3.14*height*radius**2:8.2f} gallons of water to fill this pool."]
answered Oct 3, 2019 at 0:50
TyberiusTyberius
6022 gold badges10 silver badges19 bronze badges
Not the answer you're looking for? Browse other questions tagged python string concatenation or ask your own question.
This article describes how to concatenate strings in Python.
- Concatenate multiple strings:
+
,+=
operator - Concatenate strings and numbers:
+
,+=
operator,str[]
,format[]
, f-string - Concatenate a list of strings into one string:
join[]
- Concatenate a list of numbers into one string:
join[]
,str[]
Concatenate multiple strings: +
, +=
operator
+
operator
You can concatenate string literals ['...'
or "..."
] and string variables with the +
operator.
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
+=
operator
You can append another string to a string with the in-place operator, +=
. The string on the right is concatenated after the string variable on the left.
s1 += s2
print[s1]
# aaabbb
If you want to add a string to the end of a string variable, use the +=
operator.
s = 'aaa'
s += 'xxx'
print[s]
# aaaxxx
Concatenate by writing string literals consecutively
If you write string literals consecutively, they are concatenated.
s = 'aaa''bbb''ccc'
print[s]
# aaabbbccc
Even if there are multiple spaces or newlines with backslash \
[considered as continuation lines] between the strings, they are concatenated.
s = 'aaa' 'bbb' 'ccc'
print[s]
# aaabbbccc
s = 'aaa'\
'bbb'\
'ccc'
print[s]
# aaabbbccc
Using this, you can write long strings on multiple lines in the code.
- Write a long string on multiple lines in Python
You cannot do this for string variables.
# s = s1 s2 s3
# SyntaxError: invalid syntax
Concatenate strings and numbers: +
, +=
operator, str[]
, format[]
, f-string
The +
operation between different types raises an error.
s1 = 'aaa'
s2 = 'bbb'
i = 100
f = 0.25
# s = s1 + i
# TypeError: must be str, not int
If you want to concatenate a string and a number,
such as an integer int
or a floating point float
, convert the number to a string with str[]
and then use the +
operator or +=
operator.
s = s1 + '_' + str[i] + '_' + s2 + '_' + str[f]
print[s]
# aaa_100_bbb_0.25
Use the format[]
function or the string method format[]
if you want to convert the number format, such as zero padding or decimal places.
- string - Format Specification Mini-Language — Python 3.8.1 documentation
s = s1 + '_' + format[i, '05'] + '_' + s2 + '_' + format[f, '.5f']
print[s]
# aaa_00100_bbb_0.25000
s = '{}_{:05}_{}_{:.5f}'.format[s1, i, s2, f]
print[s]
# aaa_00100_bbb_0.25000
Of course, it is also possible to embed the value of a variable directly in a string without specifying the format, which is simpler than using the +
operator.
s = '{}_{}_{}_{}'.format[s1, i, s2, f]
print[s]
# aaa_100_bbb_0.25
In Python 3.6 and later, you can also use a formatted string literal [f-string]. It is even simpler than format[]
.
- 2. Lexical analysis - Formatted string literals — Python 3.9.4 documentation
s = f'{s1}_{i:05}_{s2}_{f:.5f}'
print[s]
# aaa_00100_bbb_0.25000
s = f'{s1}_{i}_{s2}_{f}'
print[s]
# aaa_100_bbb_0.25
Concatenate a list of strings into one string: join[]
You can concatenate a list of strings into a single string with the string method, join[]
.
- Built-in Types - str - join[] — Python 3.8.1 documentation
Call the join[]
method from 'String to insert'
and pass
[List of strings]
.
'String to insert'.join[[List of strings]]
If you use an empty string ''
, [List of strings]
is simply concatenated, and if you use a comma ,
, it makes a comma-delimited string. If a newline character \n
is used, a newline will be inserted for each string.
l = ['aaa', 'bbb', 'ccc']
s = ''.join[l]
print[s]
# aaabbbccc
s = ','.join[l]
print[s]
# aaa,bbb,ccc
s = '-'.join[l]
print[s]
# aaa-bbb-ccc
s = '\n'.join[l]
print[s]
# aaa
# bbb
# ccc
Note that other iterable objects such as tuples can be specified as arguments of join[]
.
Use split[]
to split a string separated by a specific delimiter and get it as a list. See the
following article for details.
- Split strings in Python [delimiter, line break, regex, etc.]
Concatenate a list of numbers into one string: join[]
, str[]
If you set a non-string list to join[]
, an error is raised.
l = [0, 1, 2]
# s = '-'.join[l]
# TypeError: sequence item 0: expected str instance, int found
If you want to concatenate a list of numbers [int
or float
] into a
single string, apply the str[]
function to each element in the list comprehension to convert numbers to strings, then concatenate them with join[]
.
s = '-'.join[[str[n] for n in l]]
print[s]
# 0-1-2
It can be written as a generator expression, a generator version of list comprehensions. Generator expressions are enclosed in parentheses []
, but you can omit []
if the generator expression is the only argument of a function or method.
s = '-'.join[[str[n] for n in l]]
print[s]
# 0-1-2
s = '-'.join[str[n] for n in l]
print[s]
# 0-1-2
In general, generator expressions have the
advantage of reduced memory usage compared with list comprehensions. However, since join[]
internally converts a generator into a list, there is no advantage to using generator expressions.
- python - List vs generator comprehension speed with join function - Stack Overflow
See the following article for details on list comprehensions and generator expressions.
- List comprehensions in Python