5

Is there a way in Python to cat a string and a function?

For example

def myFunction():
    a=(str(local_time[0]))
    return a

b="MyFunction"+myFunction

I get an error that I cannot concatenate a 'str' and 'function' object.

DotPi
  • 3,977
  • 6
  • 33
  • 53
user2295959
  • 51
  • 1
  • 1
  • 2

3 Answers3

15

There are two possibilities:

If you are looking for the return value of myfunction, then:

print 'function: ' + myfunction() 

If you are looking for the name of myfunction then:

print 'function: ' + myfunction.__name__ 
aldeb
  • 6,588
  • 5
  • 25
  • 48
  • I want to cll the function like b="MyFunction"+myFunction(), sorry, I missed the () above. I am used to C++. I want to run the function in the statement declaring b, is that possible? I can do it if I run myFunction() outside of declaring b but I want to do it during that declaration, is it possible in python? – user2295959 Apr 19 '13 at 12:29
  • Calling the function will return a string. See the code here: http://ideone.com/UCUMgO. – aldeb Apr 19 '13 at 12:50
3

You need to call your function so that it actually returns the value you are looking for:

b="MyFunction"+myFunction()
mjgpy3
  • 8,597
  • 5
  • 30
  • 51
-1

You can use

var= "string" + str(function())

example

a="this is best"
s="number of chars. in a " + str(len(a))
print(s)
Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37