0

Function definition is here

def printme(str):
   """This prints a string passed into this function"""
   print str

You can run it like below

printme("I'm first call to user defined function!")

I want to do that instead

printme(I can do it with this the double quote)

What should I amend in my function to be able to do this? I tried this but it did not work out

def printme(raw_input()):
    """This prints a string passed into this function"""

I got this error

File "<ipython-input-31-e1326fb445e6>", line 1
  def printme(raw_input()):
                     ^
SyntaxError: invalid syntax
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Andy K
  • 4,944
  • 10
  • 53
  • 82

3 Answers3

3

You cannot do that in Python, since that won't be a value, but just a couple of undefined variables.

Amit Gold
  • 727
  • 7
  • 22
2
  • You may try like this:

    str_val = "I'm first call to user defined function!"
    printme(str_val)
    

    You will get the desired output.

  • Using raw_input(), same way, assign it to a variable as:

    another_str_val = raw_input()
    printme(another_str_val)
    

Specifically within the function:

    def printme()
        str_val = raw_input()
        print str_val

    printme()

Hope this solves your issue.

P.S: Strings are always enclosed in quotes. FYI here

rkatkam
  • 2,634
  • 3
  • 18
  • 29
1

You could arguably do:

s = raw_input 

def printme(something):
    """ prints a string... """
    print something

And then call on your function like:

printme(s())

Although not sure that really gets what you want (which is not possible to do), although this does prompt you for input each time you call the function in that manner.

enter image description here

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • Hi @david-zemens, many thanks for the workaround. This is one of the reason why I hate asking questions on Python here. The guys are knowledgeable but are conceited with their knowledge. It feels like a witch hunt , each time I'm asking a basic question. Humility, where are thou? – Andy K Feb 20 '16 at 18:38
  • @AmitGold a definite no or a workaround. Both would have been good. David's answer fits me but I like your answer as well. +1 for you. – Andy K Feb 20 '16 at 18:41
  • 1
    @AndyK The answer isn't what you described. You described trying to use a string without quotes, and the answer is about asking the user (once the program is run) for the string. Two different things. (Only continuing this discussion because you may have misunderstood the answer) – Amit Gold Feb 20 '16 at 18:43
  • 1
    @AmitGold sometimes, [the question is not the question](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), and especially when the strictly *correct* answer is "This is not possible", *workaround* solutions may be preferable. It's up to the OP to decide which of several answers best suits his or her specific needs. – David Zemens Feb 20 '16 at 18:48
  • @DavidZemens in this case the answer isn't exactly an answer either. The answer doesn't do what the question was about. The question was about using a string inside the code without quotes, while the answer includes the user typing the string after the program starts running. – Amit Gold Feb 20 '16 at 18:50
  • @DavidZemens this is the story of my life. I need to read carefully about the article you posted – Andy K Feb 21 '16 at 10:23