0
print("ax^2 + bx + c = d what is your values for them? ")
a = int(input(">a = "))
b = int(input(">b = "))
c = int(input(">c = "))
d = int(input(">d = "))

given_parabola = str(a) + "x^2 + " + str(b) + "x + " + (str(c)) + " = " + str(d)

Is there any other way that I can merge integer variables with strings?

4 Answers4

3

The "best" approach really depends on what you're trying to do.

1. Concatenating lists with variable number of items (numbers and strings)

If you simply want to form a string from numbers and strings, I would first create a generator with generator expression and then join the strings with the join() method.

In [1]: a = [2, 'a', 3, 'x', 'foo', 8, 55]

In [2]: g = (str(x) for x in a)

In [3]: ' '.join(g)
Out[3]: '2 a 3 x foo 8 55'

Pluses

  • Can be used to concatenate any amount of strings and numbers, which can be in any order

Minuses

  • Probably not the most speed optimized, if you know more about the variables you are going to concatenate

2. Literal String interpolation

If you know what amount of numeric variables you want to concatenate with what strings, the problem is called string interpolation.

In Python 3.6+ you can use so-called f-strings to form string using a string template and a fixed number of variables. For example:

In [1]: a, b, c, d = 3, 2, 1, 5

In [2]: f"{a}x^2 + {b}x + {c} = {d}"
Out[2]: '3x^2 + 2x + 1 = 5'

Pluses

  • Probably the most speed optimized way to create a string from a template.

Minuses

  • This is not a general approach to "sum"/concatenate any amount of strings and numbers.

3. Using sympy for expression generation

Since your problem looks like being very specific: You want to create string from mathematical formula, you might want to look at sympy.

Installation

pip install sympy

Simple example

In [1]: from sympy import symbols, Eq, mathematica_code

In [2]: x, a, b, c, d = symbols('x a b c d')

In [3]: expr = Eq(a*(x**2) + b*x + c, d)

In [4]: var_dict = dict(a=3, b=2, c=1, d=5)

In [5]: expr_with_numbers = expr.subs(var_dict)

In [6]: mathematica_code(expr_with_numbers).replace('==', '=')
Out[6]: '3*x^2 + 2*x + 1 = 5'

you can also solve for the expression easily:

In [7]: solve(expr_with_numbers, x)
Out[7]: [-1/3 + sqrt(13)/3, -sqrt(13)/3 - 1/3]

and you can print any kind of equation. For example

In [1]: from sympy import symbols, Eq, mathematica_code, sqrt, pretty, solve

In [2]: expr = Eq(a*(x**2)/(sqrt(x-c)), d)

In [3]: var_dict = dict(a=3, b=2, c=1, d=5)

In [4]: expr_with_numbers = expr.subs(var_dict)

In [5]: print(pretty(expr_with_numbers, use_unicode=False))
      2
   3*x
--------- = 5
  _______
\/ x - 1

Pros

  • Useful, if you want to create complex mathematical expressions
  • Can also output pretty multiline output or even LaTeX output.
  • Can be useful if you want to actually solve the equation, too

Cons

  • Not speed-optimized for simple string formation.
Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
  • I can't imagine this approach would be very performant even compared to regular concatenation. It also won't produce the results OP was specifically looking for as-is. – Abion47 Jul 09 '20 at 19:30
  • I guess your comment is regarding to the first option. Answering to new python questions is always a speed game, hence I was updating the answer one bit at a time. The answer has been updated to have different options for different use cases (all with their own pros and cons) – Niko Föhr Jul 10 '20 at 12:47
2

Might I suggest string interpolation?

given_parabola = "%sx^2 + %sx + %s = %s" % (a, b, c, d)

Or

given_parabola = f"{a}x^2 + {b}x + {c} = {d}"
10 Rep
  • 2,217
  • 7
  • 19
  • 33
Abion47
  • 22,211
  • 4
  • 65
  • 88
2

You can avoid concatenating multiple strings using the format string python proposed.

Using Format strings vs concatenation to do a list of more performant to less performant

  • f-string as f"{a}x^2 + {b}x + {c} = {d}"
  • "%sx^2 + %sx + %s = %s" % (a,b,c,d)
  • "{}x^2 + {}x + {} = {}".format(a,b,c,d)
10 Rep
  • 2,217
  • 7
  • 19
  • 33
azro
  • 53,056
  • 7
  • 34
  • 70
0

Yes, hopefully, this is what you mean:

# This way the integer 10 will convert to a string automatically. Works in Print as well!
x = 10
y = "lemons"
z = "In the basket are %s %s" % (x, y)
print(z)

Output:

In the basket are 10 lemons

10 Rep
  • 2,217
  • 7
  • 19
  • 33