Home > OS >  what options to convert this to a string?
what options to convert this to a string?

Time:06-19

I need following desired output so that I can copy and paste into another program. Not sure how to use the correct string syntax.

A = 10 
B = 20
u = np.linspace(0, 2* np.pi*57.3, 1000)
print((A B)*cos(u))

Output:

30

Desired Output

(10 20)*cos(u)

CodePudding user response:

You can use the .format() function to insert certain variable values into a string by writing {} in it whenever you need to add those values:

import numpy as np

A = 10 
B = 20
u = np.linspace(0, 2* np.pi*57.3, 1000)
print((A B)*np.cos(u))

print('({} {})*cos(u)'.format(A, B))

Output:

(10 20)*cos(u)

CodePudding user response:

print('(%.d   %.d)*cos(u)' %(A, B))

CodePudding user response:

Easier way for .format():

import numpy as np

A = 10 
B = 20
u = np.linspace(0, 2* np.pi*57.3, 1000)

print(f'({A} {B})*cos({u})')

OUTPUT:
(10 20)*cos("value of u")

  • Related