user2295959
user2295959

Reputation: 61

Python Concatenate String and Function

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.

Upvotes: 6

Views: 13926

Answers (3)

abdurehman
abdurehman

Reputation: 1

You can use

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

example

a="this is best"
s="number of chars. in a " + str(len(a))
print(s)

Upvotes: -1

aldeb
aldeb

Reputation: 6836

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__ 

Upvotes: 15

mjgpy3
mjgpy3

Reputation: 8957

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

b="MyFunction"+myFunction()

Upvotes: 3

Related Questions