JohnDotOwl
JohnDotOwl

Reputation: 3755

Syntax Error Python Text with variable

def postLoadItemUpdate(itemid):
    r = requests.post("http://www.domain.com/ex/s/API/r/postLoadItemUpdate?id='".itemid."'")
    print(r.text)

what is wrong with '".itemid."'"

There seems to be an syntax error there.

Upvotes: 0

Views: 107

Answers (6)

Nafiul Islam
Nafiul Islam

Reputation: 82450

Alternatively, you could also use format:

r = requests.post("http://www.domain.com/ex/s/API/r/postLoadItemUpdate?id={0}".format(itemid))

In your particular use case, formal seems to be more flexible, and url changes will have little impact.

Upvotes: 1

Dan L
Dan L

Reputation: 325

Where to start: does "constant string".itemid."constant string 2" work in Python?

You need to concatenate strings differently. Interactive mode for Python is your friend: learn to love it:

    $ python
    Python 2.7.5 (default, Aug 25 2013, 00:04:04)
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> foo = "-itemid-"
    >>> "string1" + foo + "string2"
    'string1-itemid-string2'

That should give you a starting point.

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

In Python use + operator for string concatenation:

"http://www.domain.com/ex/s/API/r/postLoadItemUpdate?id='" + itemid + "'"

But for string concatenation itemid should be a string object, otherwise you need to use str(itemid).

Another alternative is to use string formatting, here type conversion is not required:

"http://www.domain.com/ex/s/API/r/postLoadItemUpdate?id='{}'".format(itemid)

Upvotes: 1

user1907906
user1907906

Reputation:

String concatenation in Python works like this

s + itemId + t

not like this:

s . itemid . t

Upvotes: 1

thefourtheye
thefourtheye

Reputation: 239443

To concatenate strings you have to use + and if itemid is not a string value, you might want to apply str to convert that to a string.

"http://www.domain.com/ex/s/API/r/postLoadItemUpdate?id='" + str(itemid) + "'"

Upvotes: 1

mdml
mdml

Reputation: 22872

If you're looking to concatenate the strings, use the + operator:

r = requests.post("http://www.domain.com/ex/s/API/r/postLoadItemUpdate?id='" + itemid + "'")

Upvotes: 1

Related Questions